Underscore Variables
There’s one more thing to say about variables. The special syntax
_VarName
is used for a normal variable, not an anonymous
variable. Normally the compiler will generate a warning if a
variable is used only once in a clause since this is usually the
sign of an error. If the variable is used only once but starts
with an underscore, the warning message will not be generated.
Since _Var
is a normal variable, very subtle bugs can be
caused by forgetting this and using it as a “don’t
care” pattern. In a complicated pattern match, it
can be difficult to spot that, for
example, _Int
is repeated when it shouldn’t have been,
causing the pattern match to fail.
There are two main uses of underscore variables.
-
To name a variable ...
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.