diff options
author | Kaushal Modi <kaushal.modi@gmail.com> | 2021-11-03 03:08:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-03 08:08:50 +0100 |
commit | dbbafd320ce1edb0e51167c8e9f6ba3d865672c3 (patch) | |
tree | c80d2547235fc9f04a4ecd37f748d618fd872e91 | |
parent | 5fed1c05ce7ea9cbeb2b70bb8583e836c981abd4 (diff) | |
download | Nim-dbbafd320ce1edb0e51167c8e9f6ba3d865672c3.tar.gz |
manual: Document that comma propagates the default values of parameters (#19080)
* manual: Document that comma propagates the default values of parameters Fixes https://github.com/nim-lang/Nim/issues/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.
-rw-r--r-- | doc/manual.rst | 10 | ||||
-rw-r--r-- | tests/proc/t15949.nim | 20 |
2 files changed, 29 insertions, 1 deletions
diff --git a/doc/manual.rst b/doc/manual.rst index 1371dba17..001402036 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -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`. diff --git a/tests/proc/t15949.nim b/tests/proc/t15949.nim new file mode 100644 index 000000000..bc3fddc84 --- /dev/null +++ b/tests/proc/t15949.nim @@ -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) |