Update docs to reflect pattern syntax change
This commit is contained in:
parent
d887c0e789
commit
90cd795b8b
2 changed files with 7 additions and 15 deletions
|
@ -2702,7 +2702,7 @@ arbitrary depth.
|
||||||
A @dfn{declaration statement} is one that introduces a @emph{name} into the
|
A @dfn{declaration statement} is one that introduces a @emph{name} into the
|
||||||
enclosing statement block. The declared name may denote a new slot or a new
|
enclosing statement block. The declared name may denote a new slot or a new
|
||||||
item. The scope of the name extends to the entire containing block, both
|
item. The scope of the name extends to the entire containing block, both
|
||||||
before and after the declaration.
|
before and after the declaration. Tag names may not be shadowed by variable names.
|
||||||
|
|
||||||
@menu
|
@menu
|
||||||
* Ref.Stmt.Decl.Item:: Statement declaring an item.
|
* Ref.Stmt.Decl.Item:: Statement declaring an item.
|
||||||
|
@ -3274,7 +3274,7 @@ alt x @{
|
||||||
cons(10, _) @{
|
cons(10, _) @{
|
||||||
process_ten();
|
process_ten();
|
||||||
@}
|
@}
|
||||||
nil. @{
|
nil @{
|
||||||
ret;
|
ret;
|
||||||
@}
|
@}
|
||||||
_ @{
|
_ @{
|
||||||
|
@ -3283,12 +3283,6 @@ alt x @{
|
||||||
@}
|
@}
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
Note in the above example that @code{nil} is followed by a period. This is
|
|
||||||
required syntax for pattern matching a nullary tag variant, to distingush the
|
|
||||||
variant @code{nil} from a binding to variable @code{nil}. Without the period
|
|
||||||
the value of @code{x} would be bound to variable @code{nil} and the compiler
|
|
||||||
would issue an error about the final wildcard case being unreachable.
|
|
||||||
|
|
||||||
Records can also be pattern-matched and their fields bound to variables.
|
Records can also be pattern-matched and their fields bound to variables.
|
||||||
When matching fields of a record, the fields being matched are specified
|
When matching fields of a record, the fields being matched are specified
|
||||||
first, then a placeholder (@code{_}) represents the remaining fields.
|
first, then a placeholder (@code{_}) represents the remaining fields.
|
||||||
|
|
|
@ -155,18 +155,16 @@ patterns, as in this definition of `area`:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
For variants without arguments, you have to write `variantname.` (with
|
Another example:
|
||||||
a dot at the end) to match them in a pattern. This to prevent
|
|
||||||
ambiguity between matching a variant name and binding a new variable.
|
|
||||||
|
|
||||||
# type point = {x: float, y: float};
|
# type point = {x: float, y: float};
|
||||||
# enum direction { north; east; south; west; }
|
# enum direction { north; east; south; west; }
|
||||||
fn point_from_direction(dir: direction) -> point {
|
fn point_from_direction(dir: direction) -> point {
|
||||||
alt dir {
|
alt dir {
|
||||||
north. { {x: 0f, y: 1f} }
|
north { {x: 0f, y: 1f} }
|
||||||
east. { {x: 1f, y: 0f} }
|
east { {x: 1f, y: 0f} }
|
||||||
south. { {x: 0f, y: -1f} }
|
south { {x: 0f, y: -1f} }
|
||||||
west. { {x: -1f, y: 0f} }
|
west { {x: -1f, y: 0f} }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue