Update docs to reflect pattern syntax change

This commit is contained in:
Tim Chevalier 2012-01-19 01:22:19 -08:00
parent d887c0e789
commit 90cd795b8b
2 changed files with 7 additions and 15 deletions

View file

@ -2702,7 +2702,7 @@ arbitrary depth.
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
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
* Ref.Stmt.Decl.Item:: Statement declaring an item.
@ -3274,7 +3274,7 @@ alt x @{
cons(10, _) @{
process_ten();
@}
nil. @{
nil @{
ret;
@}
_ @{
@ -3283,12 +3283,6 @@ alt x @{
@}
@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.
When matching fields of a record, the fields being matched are specified
first, then a placeholder (@code{_}) represents the remaining fields.

View file

@ -155,18 +155,16 @@ patterns, as in this definition of `area`:
}
}
For variants without arguments, you have to write `variantname.` (with
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.
Another example:
# type point = {x: float, y: float};
# enum direction { north; east; south; west; }
fn point_from_direction(dir: direction) -> point {
alt dir {
north. { {x: 0f, y: 1f} }
east. { {x: 1f, y: 0f} }
south. { {x: 0f, y: -1f} }
west. { {x: -1f, y: 0f} }
north { {x: 0f, y: 1f} }
east { {x: 1f, y: 0f} }
south { {x: 0f, y: -1f} }
west { {x: -1f, y: 0f} }
}
}