summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorPMunch <peterme@peterme.net>2020-05-12 20:28:18 +0200
committerGitHub <noreply@github.com>2020-05-12 20:28:18 +0200
commit9acbf99efb42d7d3f98f3d1d6677923fc4dd3a28 (patch)
tree285412bf78787fd66f2719c84eacf0f6d1bd90a4 /doc
parent82f008158cbc023c5e20103bfb4bda317be52fb2 (diff)
downloadNim-9acbf99efb42d7d3f98f3d1d6677923fc4dd3a28.tar.gz
Allow let to not have value when using importc (#14258)
* Allow let to not have value when using importc

This allows a let statement with the `{.importc.}` pragma to not be
initialised with a value. This allows us to declare C constants as Nim
lets without putting the value in the Nim code (which can lead to
errors, and requires us to go looking for the value). Fixes #14253

* Proper fix and documentation + changelog entry

* Improve testcase with one from timotheecour

* Add test to verify it working with macros
Diffstat (limited to 'doc')
-rw-r--r--doc/manual.rst15
1 files changed, 15 insertions, 0 deletions
diff --git a/doc/manual.rst b/doc/manual.rst
index 82678f40e..8d4439644 100644
--- a/doc/manual.rst
+++ b/doc/manual.rst
@@ -2679,6 +2679,11 @@ nor can their address be taken. They cannot be assigned new values.
 
 For let variables the same pragmas are available as for ordinary variables.
 
+As ``let`` statements are immutable after creation they need to define a value
+when they are declared. The only exception to this is if the ``{.importc.}``
+pragma (or any of the other ``importX`` pragmas) is applied, in this case the
+value is expected to come from native code, typically a C/C++ ``const``.
+
 
 Tuple unpacking
 ---------------
@@ -7090,6 +7095,16 @@ spelled*:
 .. code-block::
   proc printf(formatstr: cstring) {.header: "<stdio.h>", importc: "printf", varargs.}
 
+When ``importc`` is applied to a ``let`` statement it can omit its value which
+will then be expected to come from C. This can be used to import a C ``const``:
+
+.. code-block::
+  {.emit: "const int cconst = 42;".}
+
+  let cconst {.importc, nodecl.}: cint
+
+  assert cconst == 42
+
 Note that this pragma has been abused in the past to also work in the
 js backend for js objects and functions. : Other backends do provide
 the same feature under the same name. Also, when the target language