Skip to content

Commit

Permalink
manual: Document the use of static as a proc call
Browse files Browse the repository at this point in the history
Also adds tests.

Fixes nim-lang#16987 .
  • Loading branch information
kaushalmodi committed Nov 2, 2021
1 parent b155864 commit 8df50d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions doc/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2975,6 +2975,21 @@ Even some code that has side effects is permitted in a static block:
static:
echo "echo at compile time"
When `static` is used like a procedure call with a proc call as an argument,
that proc call gets executed at compile-time.

.. code-block:: nim
proc getNum(a: int): int = a
# Below calls "echo getNum(123)" at compile time.
static:
echo getNum(123)
# Below call evaluates the "getNum(123)" at compile time, but its
# result gets used at run time.
echo getNum(123).static
There are limitations on what Nim code can be executed at compile time;
see `Restrictions on Compile-Time Execution
<#restrictions-on-compileminustime-execution>`_ for details.
Expand Down
14 changes: 14 additions & 0 deletions tests/system/tstatic_callable.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# issue# 16987

proc getNum(a: int): int = a

# Below calls "doAssert getNum(123) == 123" at compile time.
static:
doAssert getNum(123) == 123

# Below calls evaluate the "getNum(123)" at compile time, but the
# results of those calls get used at run time.
doAssert getNum(123).static == 123
doAssert getNum(123).static() == 123
doAssert (static getNum(123)) == 123
doAssert (static(getNum(123))) == 123
14 changes: 14 additions & 0 deletions tests/system/tstatic_callable_error.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# issue# 16987

discard """
errormsg: "cannot evaluate at compile time: inp"
nimout: '''
tstatic_callable_error.nim(14, 21) Error: cannot evaluate at compile time: inp'''
"""


# line 10
proc getNum(a: int): int = a

let inp = 123
echo (static getNum(inp))

0 comments on commit 8df50d8

Please sign in to comment.