Skip to content

Commit

Permalink
manual: Document that comma propagates the default values of paramete…
Browse files Browse the repository at this point in the history
…rs (nim-lang#19080)

* manual: Document that comma propagates the default values of parameters

Fixes nim-lang#15949.

* Use the "bug #NNNN" comment syntax for consistency

Ref:
https://nim-lang.github.io/Nim/contributing.html#writing-tests-stdlib

> Always refer to a GitHub issue using the following exact syntax: bug
for tooling.
  • Loading branch information
kaushalmodi authored and PMunch committed Mar 28, 2022
1 parent 63c8cdd commit 3ce926b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion doc/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3603,9 +3603,17 @@ does not provide a value for the argument. The value will be reevaluated
every time the function is called.

.. code-block:: nim
# b is optional with 47 as its default value
# b is optional with 47 as its default value.
proc foo(a: int, b: int = 47): int
Just as the comma propagates the types from right to left until the
first parameter or until a semicolon is hit, it also propagates the
default value starting from the parameter declared with it.

.. code-block:: nim
# Both a and b are optional with 47 as their default values.
proc foo(a, b: int = 47): int
Parameters can be declared mutable and so allow the proc to modify those
arguments, by using the type modifier `var`.

Expand Down
20 changes: 20 additions & 0 deletions tests/proc/t15949.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# bug #15949

discard """
errormsg: "parameter 'a' requires a type"
nimout: '''
t15949.nim(20, 14) Error: parameter 'a' requires a type'''
"""


# line 10
proc procGood(a, b = 1): (int, int) = (a, b)

doAssert procGood() == (1, 1)
doAssert procGood(b = 3) == (1, 3)
doAssert procGood(a = 2) == (2, 1)
doAssert procGood(a = 5, b = 6) == (5, 6)

# The type (and default value propagation breaks in the below example
# as semicolon is used instead of comma.
proc procBad(a; b = 1): (int, int) = (a, b)

0 comments on commit 3ce926b

Please sign in to comment.