case statement is an extension of Coconut’s
match statement for performing multiple
match statements against the same value, where only one of them should succeed. Unlike lone
match statements, only one
match statement inside of a
case block will ever succeed, and thus more general matches should be put below more specific ones.
Each pattern in a
case block is checked until a match is found, and then the corresponding body is executed, and the
case block terminated. The syntax for
case blocks is
case <value>: match <pattern> [if <cond>]: <body> match <pattern> [if <cond>]: <body> ... [else: <body>]
<pattern> is any match pattern,
<value> is the item to match against,
<cond> is an optional additional check, and
<body> is simply code that is executed if the header above it succeeds. Note the absence of an in in the match statements: that’s because the
<value> in case
<value> is taking its place.
In Python can’t be done without a long series of checks for each match statement. See the compiled code for the Python syntax.
Al solito codice difficile da leggere, peraltro OK:
In Coconut, the keywords
async (keyword in Python 3.5), and
await (keyword in Python 3.5) are also valid variable names. While Coconut can disambiguate these two use cases, when using one of these keywords as a variable name, a backslash is allowed in front to be explicit about using a keyword as a variable name.
In Coconut, all variable names starting with
_coconut are reserved. The Coconut compiler will modify and reference these variables with the assumption that the code being compiled does not modify them in any way. If your code does modify any such variables, your code is unlikely to work properly.