7 Miscellaneous
In this chapter, a number of miscellaneous features of Erlang are described.
7.1 Token Syntax
In Erlang 4.8 (OTP R5A) the syntax of Erlang tokens have been extended to allow the use of the full ISO-8859-1 (Latin-1) character set. This is noticeable in the following ways:
- All the Latin-1 printable characters can be used and are shown without the escape backslash convention.
- Atoms and variables can use all Latin-1 letters.
The new characters from Latin-1 have the following classifications in Erlang:
Octal Decimal Class 200 - 237 128 - 159 Control characters 240 - 277 160 - 191 - ¿ Punctuation characters 300 - 326 192 - 214 À - Ö Uppercase letters 327 215 × Punctuation character 330 - 336 216 - 222 Ø - Þ Uppercase letters 337 - 366 223 - 246 ß - ö Lowercase letters 367 247 ÷ Punctuation character 370 - 377 248 - 255 ø - ÿ Lowercase letters Character classes 7.2 String concatenation
Two adjacent string literals are concatenated into one. This is done already at compile-time, and doesn't incur any runtime overhead. Example:
"string" "42"is equivalent to
"string42"This feature is convenient in at least two situations:
- when one of the strings is the result of a macro expansion;
- when a string is very long, and would otherwise either have to wrap, making the source code harder to read, or force the use of some runtime append operation.
7.3 The ++ list concatenation operator
Since list concatenation is a very common operation, it is convenient to have a terse way of expressing it. The ++ operator appends its second argument to its first. Example:
X = [1,2,3], Y = [4,5], X ++ Y.results in
[1,2,3,4,5]
.The ++ operator has precedence between the binary '+' operator and the comparison operators.
7.4 The -- list subtraction operator
The -- operator produces a list which is a copy of the first argument, subjected to the following procedure: for each element in the second argument, its first occurrence in the first argument is removed.
X = [1,2,3,2,1,2], Y = [2,1,2], X -- Y.results in
[3,1,2]
.The -- operator has precedence between the binary '+' operator and the comparison operators.
7.5 Bitwise operator bnot
Apart from the binary bitwise operators
band
,bor
andbxor
, there is a unary operatorbnot
with the same precedence as the other unary operators + and -, i.e., higher than the binary operators. Example:bnot 7.7.6 Logical operators
The atoms
true
andfalse
are usually used for representing Boolean values. With the binary operatorsand
,or
andxor
, and the unary operatornot
, Boolean values can be combined. Example:M1 = lists:member(A, List1), M2 = lists:member(A, List2), M1 and M2.Note that the operators are strict, i.e., they always evaluate both their arguments.
not
has the same priority as the other unary operators. The binary logical operators have precedence between the=
operator and the comparison operators, theand
operator having higher precedence thanor
andxor
.7.7 Match operator = in patterns
This extension was added in Erlang 4.8 (OTP R5A).
The = operator is also called the `match' operator. The match operator can now be used in a pattern, so that
P1 = P2
is a valid pattern, where bothP1
andP2
are patterns. This compound pattern when matched against a term causes the term to be matched against bothP1
andP2
.One use for this construction is to avoid reconstructing a term which was part of an argument to a function. Example:
f({'+',X,Y}=T) -> {X+Y,T}.It also makes it possible to rewrite the construction
f(X) when X == #rec{x=1, y=a} -> ...as
f(#rec{x=1, y=a} = X) -> ...In the absence of optimization for the former case, the latter case is more efficient.
7.8 Literal string prefix in patterns
This extension was added in Erlang 4.8 (OTP R5A).
A new construction is allowed in patterns, namely a literal string as the first operand of the ++ operator. Example:
f("prefix" ++ L) -> ...This is syntactic sugar for the equivalent, but harder to read
f([$p,$r,$e,$f,$i,$x | L]) -> ...7.9 Disjunctions in guards
This extension was added in Erlang 4.9 (OTP R6A).
A new construction is allowed in guards, the disjunction operator ';'. The construction is syntactic sugar which removes the bother of writing the same body after several guards.
f(X) when xxx ; yyy ; zzz -> pop(X).This is syntactic sugar for the equivalent
f(X) when xxx -> pop(X); f(X) when yyy -> pop(X); f(X) when zzz -> pop(X).The abstract format has been changed accordingly to contain a list of (conjunctive) guards where there was previously only one guard.
7.10 Expressions in patterns
This extension was added in Erlang 5.0 (OTP R7A).
An arithmetic expression can be used within a pattern, if it uses only numeric or bitwise operators, and if its value can be evaluated to a constant at compile-time. This is especially useful when the expression is defined by a macro.
Example:
case X of {1+2, T} -> T end.