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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access