Clause
← All documentation

Patterns, clauses and body matches

Function clause heads, body matches, case clauses and if guards are executable for every admitted term kind (terms). Legality and availability are checked separately: invalid Erlang is a semantic error even in unreachable code; legal code that needs a missing feature gets a capability diagnostic; a runtime mismatch is never a compiler error.

ContextStatus
Function heads + guardsImplemented; exhaustion raises error:function_clause
Body matches and sequences, begin/endImplemented; failure raises error:{badmatch, RHS}
case clauses + guardsImplemented; exhaustion raises error:{case_clause, Value}
if guard clausesImplemented; exhaustion raises error:if_clause
maybe with ?= and else clausesImplemented; a failed ?= yields its value or selects an else clause, whose exhaustion raises error:{else_clause, Value}; needs the maybe_expr feature (preprocessor)
List, binary and map comprehensionsImplemented (below)
catch ExprImplemented; no patterns (ABI)
try of and catch clausesImplemented; of exhaustion raises error:{try_clause, Value}, unmatched exceptions re-raise and after runs on every path (ABI); Class:Reason:Stack binds the stack trace
Fun clausesCapability (F18)
receiveCapability (F22/F25)

Comprehensions

[T1, ..., Tn || Q1, ..., Qm], << T || Q1, ... >> and #{K => V, ... || Q1, ... } follow OTP 29:

Each generator is a loop in the function body whose input cursor (a list or bitstring rest, or a map and a position) lives in frame term slots; the produced elements accumulate reversed in another term slot and become the list, the joined bitstring or the map once at the end, so long inputs need constant native and process stack.

Pattern forms

FormRuleRejection
_Never binds; occurrences independentReading _ is semantic
Name, _Name, repeatsSingle assignment; repeats need exact equality (1 ≠ 1.0)Unbound/unsafe read semantic; inequality mismatches
P1 = P2, parenthesesBoth constrain the same value; no key/size bindings between siblingsIllegal sibling dependency semantic
Atoms, integers, chars, floatsExact equality; signed zero distinctMismatch
Constant arithmeticOTP pattern operators folded exactly; 1 div 0 and non-constants rejectedSemantic
Tuples, lists, improper tails, stringsExact arity and cons/nil shape; strings are listsMismatch
"prefix" ++ TailLiteral string or literal integer list prefix onlyVariable prefix semantic
#{K := P}:= only; extra keys allowed; #{} tests type; keys are guard expressions over incoming bindings=>, unbound key semantic; missing key mismatches
BitstringsValidated type/size/unit; earlier segments may size later ones; unsized tail lastInvalid specifier semantic; short data mismatches
Tuple records #r{f = P}, #r.fExpanded to tuple constraints; omitted fields unconstrainedUnknown record/field semantic
Local native records #r{f = P}Record of this module named r, then each listed field (a field it lacks fails)Unknown record semantic
Qualified/imported native records #m:r{f = P}Record of module m named r; exported when a field is listed; then each fieldNone at compile time
Anonymous records #_{f = P}Any native record; exported or defined in this module when a field is listed#_{...} as an expression is an error
Calls, variable arithmetic, other expressionsNot patternsSemantic

Scopes

Map keys and binary sizes read the incoming environment, never siblings: #{K := V} = #{key := K} and <<X:N>> = <<N:8>> cannot bind their own key or size. Inside one binary, a size may read earlier segments: <<N:8, X:N>> is legal; {N, <<X:N>>} needs N bound beforehand. Constant bad arithmetic in a key or size is legal and fails at match time.

Binary validation rejects nested-container/alias segments, conflicting modifiers, bad unit ranges, unit without size for integer/float defaults, invalid UTF size/unit, typed/sized literal strings and non-final unsized binary segments.

Execution

Limits

Pattern normalization shares the module's 1,000,000-unit semantic budget (an integer costs about its decimal digits); a constant past the integer limit of 4,194,240 bits is an illegal pattern, as in OTP (terms). Each match plan has a 100,000-work ceiling. The parser separately limits nesting to 256 (hard ceiling 512).