Patterns
The following is a list of expressions that have special meaning when treated as a pattern, whether they're used in a message pattern role, or match:
, or with =
. They are always parsed as normal expressions, and usually converted into a Pattern
at the macroexpansion phase, via to: Pattern
.
See also: =
, :=
, Defining Methods.
Types of Patterns
_
,foo
, ...Possibly-named wildcard matches.
1
,2/3
,4.0
,$a
,True
,False
,"foo"
, ...Primitive literal values match only on themselves.
foo: pattern
A named pattern-match. Matches
pattern
, binding the value it matches tofoo
.head . tail
Matches a non-empty list, matching its first value on
head
and the rest of it ontail
.[]
,[pattern, pattern-2]
, ...Matches a list of fixed length, recursively matching each
pattern
on each of its values.()
,(pattern, pattern-2)
, ...Matches a tuple, recursively matching each
pattern
on each of its values.@foo
,@(foo: x)
,@(foo: x bar: _)
, ...Matches a particle value. If it's a single particle, it does regular equality checking; if it's a keyword particle, it recursively pattern-matches the particle's values. A wildcard in a particle pattern matches placeholders, but no other pattern will.
`a
,`(1 + ~b)
, ...Matches expression values recursively. Unquotes serve as named wildcard patterns, with the same recursive semantics as quasiquotation.
-> pattern
Matches any object delegating to something matching
pattern
. Often used to check if something is an "instance" of some object.For primitive values, it checks if the value itself matches the pattern, so
[1, 2]
will match(-> List)
.NOTE: This is only really useful in
match:
and in message patterns; when used with=
the objects aren't evaluated.== pattern
Matches with strict equality only. This is different from regular matching as it doesn't follow delegates to see if objects match.
... pattern
Matches a variadic role, matching the list of values as
pattern
.That is, with a single value given for a variadic role,
pattern
will be pattern-matching a list with that single value. A role with more than one value given, via tuples, is matched as a list containing all of the values. A role with zero values (the empty tuple,()
) is matched as an empty list.
A Pattern's Object
The object onto which a method is inserted is determined by the type of the pattern in its message pattern's first role:
_
,foo
,bar
, ...1
,2
, ...$a
,$b
, ...Character
True
,False
1.0
,2.0
, ...1/2
,3/4
, ...""
,"foo"
, ...String
head . tail
,[]
,[pattern, pattern-2]
, ...`x
,`(1 + ~y)
, ...Expression
@foo
,@(bar: 2 baz: _)
, ...foo: pattern
target of
pattern
-> pattern
target of
pattern
== pattern
target of
pattern
... pattern
Foo
,Foo bar
,{ expr; expr-2 }
, ...the object itself, or the object yielded by the block's execution
Thus, 1 foo: $a := x
is a definition placed on Integer
, while (... args) foo
is inserted on Object
.
Note that roles beginning with a capital letter are assumed to be targeting a particular object. That is, Foo
means the same as { Foo }
, while foo
is just a named wildcard targeting Object
.
Above, the first definition is placed on the particular object Foo
(assuming it's defined), while the second definition is placed on the Foo
object as well as Object
(the second role, a named wildcard), binding the second role value as baz
.
To define on a particular object that isn't capitalized, wrap it in a block:
As in the first example, foo bar
matches any object, while { foo } bar
is only defined on foo
(the result of the block's execution). { 1 + 1 } bar: Foo
is defined on 2
and Foo
.
Pattern Objects
On occasion you'll want to create a pattern-match from an expression, or use patterns manually, usually in macros you define. The following methods should come in handy.
expr to: Pattern → Pattern | expr is-a?: Expression
Convert an expression to the pattern-match it represents.
If the expression cannot represent a pattern, @unknown-pattern:
is signaled.
expr to: Pattern Role → Pattern | expr is-a?: Expression
Convert an expression to the pattern-match it represents, as if it were a role in a message pattern. The main difference is that a capitalized dispatch is considered a "particlar object" rather than a named wildcard. See Syntax.
If the expression cannot represent a pattern, @unknown-pattern:
is signaled.
expr to: Pattern Define → Pattern | expr is-a?: Expression
Convert an expression to a message pattern for a definition, using @(to: Pattern Role)
for its targets.
If the expression cannot represent a message pattern, @unknown-pattern:
is signaled.
pat name → String | pat is-a?: Pattern
Yields the name of a named wildcard match or a single message pattern.
pat match: v → in?: [@(ok: bindings), @none] | pat is-a?: Patterh
Attempts to match the value v
with pattern pat
. If value matches, the bindings from the match are yielded, wrapped in @ok:
. Otherwise, @none
is yielded.