Skip to content

Commit 60cbdbf

Browse files
authored
close #18092 document elif in case statements (#18105)
* close #18092 [skip ci] document elif in case statements * fixup * clarify spec; mention special rule for string in case statements * address comments
1 parent a36efb5 commit 60cbdbf

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

doc/manual.rst

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2981,11 +2981,13 @@ Example:
29812981

29822982
.. code-block:: nim
29832983
2984-
case readline(stdin)
2984+
let line = readline(stdin)
2985+
case line
29852986
of "delete-everything", "restart-computer":
29862987
echo "permission denied"
29872988
of "go-for-a-walk": echo "please yourself"
2988-
else: echo "unknown command"
2989+
elif line.len == 0: echo "empty" # optional, must come after `of` branches
2990+
else: echo "unknown command" # ditto
29892991
29902992
# indentation of the branches is also allowed; and so is an optional colon
29912993
# after the selecting expression:
@@ -2996,19 +2998,23 @@ Example:
29962998
else: echo "unknown command"
29972999
29983000
2999-
The `case` statement is similar to the if statement, but it represents
3001+
The `case` statement is similar to the `if` statement, but it represents
30003002
a multi-branch selection. The expression after the keyword `case` is
30013003
evaluated and if its value is in a *slicelist* the corresponding statements
30023004
(after the `of` keyword) are executed. If the value is not in any
3003-
given *slicelist* the `else` part is executed. If there is no `else`
3004-
part and not all possible values that `expr` can hold occur in a
3005-
*slicelist*, a static error occurs. This holds only for expressions of
3006-
ordinal types. "All possible values" of `expr` are determined by `expr`'s
3007-
type. To suppress the static error an `else` part with an
3008-
empty `discard` statement should be used.
3005+
given *slicelist*, trailing `elif` and `else` parts are executed using same
3006+
semantics as for `if` statement, and `elif` is handled just like `else: if`.
3007+
If there are no `else` or `elif` parts and not
3008+
all possible values that `expr` can hold occur in a *slicelist*, a static error occurs.
3009+
This holds only for expressions of ordinal types.
3010+
"All possible values" of `expr` are determined by `expr`'s type.
3011+
To suppress the static error an `else: discard` should be used.
30093012

30103013
For non-ordinal types, it is not possible to list every possible value and so
30113014
these always require an `else` part.
3015+
An exception to this rule is for the `string` type, which currently doesn't
3016+
require a trailing `else` or `elif` branch; it's unspecified whether this will
3017+
keep working in future versions.
30123018

30133019
Because case statements are checked for exhaustiveness during semantic analysis,
30143020
the value in every `of` branch must be a constant expression.
@@ -3054,15 +3060,15 @@ won't work:
30543060
var foo = Foo(x: @[])
30553061
foo.get_x().add("asd")
30563062
3057-
This can be fixed by explicitly using `return`:
3063+
This can be fixed by explicitly using `result` or `return`:
30583064

30593065
.. code-block:: nim
30603066
proc get_x(x: Foo): var seq[string] =
30613067
case true
30623068
of true:
3063-
return x.x
3069+
result = x.x
30643070
else:
3065-
return x.x
3071+
result = x.x
30663072
30673073
30683074
When statement

0 commit comments

Comments
 (0)