afterStrictly reservedTimeout or cleanup clause
Stop waiting for a message after a limit, or clean up before leaving try.
receive ... after only stops the wait when time runs out. Expressions in try ... after run before the try expression is left.
receive
Msg -> Msg
after 1000 ->
timeout
end
andStrictly reservedStrict Boolean operator
Evaluate both sides, then check whether both are true.
Both sides must be Boolean values, and the right side always runs. Use andalso when the right side should not run after a false left side.
true and false. % false
andalsoStrictly reservedShort-circuit Boolean operator
Check the left side first, and continue only when it is true.
Both sides should produce Boolean values. Put a safety check first to avoid an unnecessary or unsafe calculation on the right.
A way to picture itFirst make sure the bridge is open, then send the cart across. If the first check fails, the next step never starts.
N > 0 andalso N rem 2 =:= 0.
bandStrictly reservedBitwise operator
Compare two integers bit by bit, keeping 1 only where both bits are 1.
It works only on integers. It is often used to read masks, flags, or selected bits in a binary protocol.
6 band 3. % 2
beginStrictly reservedExpression block
Group several calculations and treat them as one expression.
Separate the steps with commas. They run in order, and the whole begin ... end expression returns the value of the last step.
begin A = 1, A + 1 end.
bnotStrictly reservedBitwise operator
Flip every bit in the binary representation of an integer.
Erlang integers have no fixed 8-bit or 32-bit limit, so the result follows arbitrary-precision integer rules.
bnot 0. % -1
borStrictly reservedBitwise operator
Compare two integers bit by bit, producing 1 wherever either bit is 1.
It works only on integers and is often used to combine independent bit flags.
4 bor 1. % 5
bslStrictly reservedBit-shift operator
Move all bits in an integer several places to the left.
With a positive shift count, this usually acts like multiplication by the matching power of 2. It accepts only integers.
3 bsl 2. % 12
bsrStrictly reservedBit-shift operator
Move all bits in an integer several places to the right.
It accepts only integers. Negative integers use an arithmetic right shift, preserving their sign meaning.
12 bsr 2. % 3
bxorStrictly reservedBitwise operator
Compare two integers bit by bit, producing 1 where the bits differ.
Matching bits produce 0 and different bits produce 1. This is an integer bit operation, not Boolean xor.
6 bxor 3. % 5
caseStrictly reservedPattern branch
Run the first branch whose pattern fits the shape of a value.
case evaluates its target once, then tries the patterns and optional guards after of from top to bottom. It raises an error if nothing matches.
A way to picture itIt is like sorting letters by the mark on each envelope. The first matching rule decides where a letter goes.
case X of
0 -> zero;
_ -> other
end.
catchStrictly reservedException capture
Catch a thrown value or error and turn it into a result that code can handle.
It appears in the older catch Expression form and in a try catch clause. New code usually prefers the clearer structure of try.
Result = catch risky().
condStrictly reservedReserved but unused
Erlang reserves this name, but it cannot currently start a cond block.
cond cannot be used directly as an atom, variable, or function name. Write 'cond' when you need that atom, and use case or if for branches.
!The current Erlang grammar does not use this word.
erl_scan:reserved_word('cond'). % true
divStrictly reservedArithmetic operator
Divide two integers and keep only the integer part.
The result is truncated toward zero, not rounded. A zero divisor raises a badarith error.
7 div 2. % 3
elseStrictly reservedmaybe failure branch
Decide what to do with a value that failed to match inside maybe.
The clauses after else still choose by pattern. This belongs only to maybe syntax; it is not a general if-else form.
!Used with maybe expressions. Older OTP releases may not support it.
maybe {ok, V} ?= fetch(), V else error -> missing end.
endStrictly reservedEnd of an expression block
Close a case, fun, if, receive, try, or another block.
Indentation cannot close these expressions. Every opened multiline block needs an explicit end.
fun(X) -> X * 2 end.
funStrictly reservedAnonymous function or function reference
Create an anonymous function, or carry an existing function as a value.
An anonymous function may have several pattern clauses. fun Module:Function/Arity refers to an existing remote function.
Double = fun(X) -> X * 2 end.
ifStrictly reservedGuard branches
Check conditions from top to bottom and run the first branch whose guard is true.
An if condition must be an allowed guard expression, not any function call. If no branch matches, Erlang raises an if_clause error.
if N > 0 -> positive; true -> other end.
letStrictly reservedReserved but unused
Erlang reserves this name, but it has no usable let expression today.
Erlang binds variables through pattern matching. If you truly need an atom named let, write it as 'let'.
!The current Erlang grammar does not use this word.
erl_scan:reserved_word('let'). % true
maybeStrictly reservedSequential matching expression
Line up several steps that may fail, and stop early when one does not match.
?= is used only inside maybe. A failed match returns the failing value, or passes it to else for a common result.
A way to picture itThink of a travel pass stamped at each stop. If one stamp is missing, the later steps wait.
!maybe expressions arrived in Erlang/OTP 25. Older releases do not support them, and early releases may require the maybe_expr feature to be enabled.
maybe
{ok, V} ?= fetch(),
V
end.
notStrictly reservedBoolean operator
Turn true into false and false into true.
not accepts only Boolean values. Passing a number, atom, or another kind of value raises a badarg error.
not false. % true
ofStrictly reservedPattern-clause marker
Introduce branches that sort a case or try result by shape.
case uses of to introduce its branches. of is optional in try, where it handles only values returned normally by the try body.
case Value of ok -> done; _ -> retry end.
orStrictly reservedStrict Boolean operator
Evaluate both sides, then check whether at least one side is true.
Both sides must be Boolean values, and the right side always runs. Use orelse when the right side should not run after a true left side.
false or true. % true
orelseStrictly reservedShort-circuit Boolean operator
Check the left side first, and continue only when it is false.
Both sides should produce Boolean values. When the left side is already true, the right side does not run, so it works well for fallback conditions.
A way to picture itTry the short road first. If it is open, there is no need to inspect the backup road.
Cached orelse filelib:is_file(Path).
receiveStrictly reservedProcess message reception
Take the first message from the current process mailbox that fits a rule.
Messages are scanned in arrival order, and unmatched messages stay in the mailbox. Add after to avoid waiting forever.
A way to picture itIt is like taking the first acceptable letter from a stack. The other letters stay in the mailbox.
receive
{ping, From} -> From ! pong
end.
remStrictly reservedArithmetic operator
Find the remainder after dividing two integers.
rem pairs with div and follows X = (X div Y) * Y + (X rem Y). A zero divisor raises an error.
7 rem 2. % 1
tryStrictly reservedException-handling expression
Handle a risky calculation, its successful result, its errors, and cleanup in one place.
try can combine of, catch, and after to handle normal values plus error, exit, and throw. An unmatched error keeps travelling outward.
try risky() catch _:Reason -> {error, Reason} end.
whenStrictly reservedGuard clause
After a pattern matches, check one more condition.
Only approved guard expressions may follow when. Ordinary functions cannot be placed there freely; a false condition moves on to the next clause.
A way to picture itFirst find the right group on the list, then check the age rule. Both checks must pass.
abs(N) when N < 0 -> -N.
xorStrictly reservedBoolean operator
Check whether exactly one of two Boolean values is true.
Both sides run and both must be Boolean values. Use bxor for an exclusive-or operation on every bit of two integers.
true xor false. % true