Skip to content

Commit 038f47e

Browse files
authored
fixes #3339 by documenting the limitations of case-statement (#13366)
1 parent 2a4aa24 commit 038f47e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

doc/manual.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,6 +2843,35 @@ expanded into a list of its elements:
28432843
of '0'..'9': echo "a number"
28442844
else: echo "other"
28452845
2846+
The ``case`` statement doesn't produce an l-value, so the following example
2847+
won't work:
2848+
2849+
.. code-block:: nim
2850+
type
2851+
Foo = ref object
2852+
x: seq[string]
2853+
2854+
proc get_x(x: Foo): var seq[string] =
2855+
# doesn't work
2856+
case true
2857+
of true:
2858+
x.x
2859+
else:
2860+
x.x
2861+
2862+
var foo = Foo(x: @[])
2863+
foo.get_x().add("asd")
2864+
2865+
This can be fixed by explicitly using ``return``:
2866+
2867+
.. code-block:: nim
2868+
proc get_x(x: Foo): var seq[string] =
2869+
case true
2870+
of true:
2871+
return x.x
2872+
else:
2873+
return x.x
2874+
28462875
28472876
When statement
28482877
--------------

0 commit comments

Comments
 (0)