Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #3339 by documenting the limitations of case-statement #13366

Merged
merged 1 commit into from
Feb 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,35 @@ expanded into a list of its elements:
of '0'..'9': echo "a number"
else: echo "other"

The ``case`` statement doesn't produce an l-value, so the following example
won't work:

.. code-block:: nim
type
Foo = ref object
x: seq[string]

proc get_x(x: Foo): var seq[string] =
# doesn't work
case true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is case true even valid? It's like saying case 123 for ints.

Copy link
Contributor

@kaushalmodi kaushalmodi Feb 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I am surprised that this is valid: https://play.nim-lang.org/#ix=2b6d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why wouldn't it? bool is an ordinal types and case statements work on ordinals.

of true:
x.x
else:
x.x

var foo = Foo(x: @[])
foo.get_x().add("asd")

This can be fixed by explicitly using ``return``:

.. code-block:: nim
proc get_x(x: Foo): var seq[string] =
case true
of true:
return x.x
else:
return x.x


When statement
--------------
Expand Down