case branches support constant sets for convenience

This commit is contained in:
Araq 2011-06-19 17:45:33 +02:00
commit 8b6f9ef5e8
5 changed files with 92 additions and 17 deletions

View file

@ -1527,6 +1527,27 @@ given, control passes after the ``case`` statement.
To suppress the static error in the ordinal case an ``else`` part with a ``nil``
statement can be used.
As a special semantic extension, an expression in an ``of`` branch of a case
statement may evaluate to a set constructor; the set is then expanded into
a list of its elements:
.. code-block:: nimrod
const
SymChars: set[char] = {'a'..'z', 'A'..'Z', '\x80'..'\xFF'}
proc classify(s: string) =
case s[0]
of SymChars, '_': echo "an identifier"
of '0'..'9': echo "a number"
else: echo "other"
# is equivalent to:
proc classify(s: string) =
case s[0]
of 'a'..'z', 'A'..'Z', '\x80'..'\xFF', '_': echo "an identifier"
of '0'..'9': echo "a number"
else: echo "other"
When statement
~~~~~~~~~~~~~~