diff --git a/system/doc/reference_manual/expressions.xml b/system/doc/reference_manual/expressions.xml index 45ca4b2092e7..8fdd1e91e300 100644 --- a/system/doc/reference_manual/expressions.xml +++ b/system/doc/reference_manual/expressions.xml @@ -138,18 +138,22 @@ member(_Elem, []) -> Name1 [H|T] {error,Reason} -
Patterns are allowed in clause heads,
Patterns are allowed in clause heads,
+
If
Pattern1 = Pattern2
When matched against a term, both
Example:
f({connect,From,To,Number,Options}, To) -> @@ -163,6 +167,11 @@ f({connect,_,To,_,_} = Signal, To) -> ...; f(Signal, To) -> ignore.+ +
The compound pattern operator does not imply that its operands
+ are matched in any particular order. That means that it is not
+ legal to bind a variable in
The following matches
@@ -214,6 +224,88 @@ answer {answer,42} 5> {C, D} = [1, 2]. ** exception error: no match of right-hand side value [1,2]+ +
Because multiple match operators are evaluated from right to left, + it means that:
+ ++Pattern1 = Pattern2 = . . . = PatternN = Expression+ +
is equivalent to:
++Temporary = Expression, +PatternN = Temporary, + . + . + ., +Pattern2 = Temporary, +Pattern = Temporary+
This is an advanced section, which references to topics not + yet introduced. It can safely be skipped on a first + reading.
The
The compound pattern operator is used to construct a + compound pattern from two patterns. Compound patterns are accepted + everywhere a pattern is accepted. A compound pattern matches if + all of its constituent patterns match. It is not legal for a + pattern that is part of a compound pattern to use variables (as + keys in map patterns or sizes in binary patterns) bound in other + sub patterns of the same compound pattern.
+Examples:
+ ++1> fun(#{Key := Value} = #{key := Key}) -> Value end. +* 1:7: variable 'Key' is unbound +2> F = fun({A, B} = E) -> {E, A + B} end, F({1,2}). +{{1,2},3} +3> G = fun(<<A:8,B:8>> = <<C:16>>) -> {A, B, C} end, G(<<42,43>>). +{42,42,10795}+ +
The match operator is allowed everywhere an expression + is allowed. It is used to match the value of an expression to a pattern. + If multiple match operators are applied in sequence, they will be + evaluated from right to left.
+ +Examples:
++1> M = #{key => key2, key2 => value}. +#{key => key2,key2 => value} +2> f(Key), #{Key := Value} = #{key := Key} = M, Value. +value +3> f(Key), #{Key := Value} = (#{key := Key} = M), Value. +value +4> f(Key), (#{Key := Value} = #{key := Key}) = M, Value. +* 1:12: variable 'Key' is unbound+ +
The expression at prompt 2> first matches the value of
+ variable
The expression at prompt 3> matches expression
+
In the expression at prompt 4> the expression
The
When evaluating an expression, the operator with the highest priority is evaluated first. Operators with the same priority are evaluated according to their associativity.