pub enum Pattern {
Field(FieldPattern),
Tuple(TuplePattern),
Value(ValuePattern),
Variable(VariablePattern),
Pair(PairPattern),
}
Expand description
A pattern that can be matched with an Expression
to enable complex flow control
and full destructuring pattern matching, which increases the flexibility and
expressivity within the language by a great degree.
Variants§
Field(FieldPattern)
A named pattern, like repeats: 4
or name: n String
.
Tuple(TuplePattern)
A pattern enclosed in parentheses, like (1 + 2)
Value(ValuePattern)
Any expression that evaluates to a value, like 1 + 2
or get_address_book()
.
Variable(VariablePattern)
A variable identifier with an optional type annotation, such as name
or name String
.
Pair(PairPattern)
A pair of patterns separated by a comma.
Implementations§
Source§impl Pattern
impl Pattern
pub fn expect_field(self) -> Result<FieldPattern, ParserError>
pub fn expect_tuple(self) -> Result<TuplePattern, ParserError>
pub fn expect_value(self) -> Result<Expression, ParserError>
pub fn expect_variable(self) -> Result<VariablePattern, ParserError>
pub fn expect_pair(self) -> Result<PairPattern, ParserError>
Source§impl Pattern
impl Pattern
Sourcepub fn linearize(&self, parser: &mut Parser, other: Pattern) -> LinearizeResult
pub fn linearize(&self, parser: &mut Parser, other: Pattern) -> LinearizeResult
Compare this pattern with another and destructure any variables if it matches.
This function is used to determine which multimethod implementation matches the arguments of a given call, applying precedence rules to ensure that the most specific patterns are chosen. In the ubiquitous Fibonacci example, it decides which of the multimethods actually get executed based on the input parameters:
def fib(0) 0
# |- high specificity
def fib(1) 1
def fib(n Int) fib(n - 1) + fib(n - 2)
# ^- low specificity