[erlang-questions] Compiler to warn on used underscore-variables

Robert Virding <>
Mon Jan 4 23:55:26 CET 2010


2010/1/4 Zoltan Lajos Kis <>

> Hello all,
>
> Currently the compiler outputs a warning if a variable is only used once.
> The "standard" way to suppress this warning is to put an underscore in front
> of the variable name.
> As a result, an underscore-variable in source codes gives us (at least for
> me) the false notion that it is not a real variable, but only a means to
> enhance code readability.
> However the compiler treats them just like ordinary variables, resulting in
> unexpected (i.e. most astonishing ;-)) no match exceptions.
>

They *ARE* ordinary variables! All variables are variables irrespective of
whether there name starts with a capital letter or an "_". The *ONLY*
exception is the variable '_' which is the anonymous or don't care variable.
It always matches and is never bound, which means it can only be used in
patterns. Or to be more correct, if it used in a term then the compiler will
complain about an unbound variable.

The special treatment of variables starting with "_" is a feature/hack in
the compiler to get around its feature/hack of warning about variables which
are never used. It was included to allow users to to give names to variables
they don't intend to use for documentation purposes. I personally don't like
and never use them, if I don't want the value I use '_'.

My question is: what is your opinion about / is it possible to modify the
> compiler so that it outputs a warning if an underscore-variable is used (not
> unused) in the code ?
>

It is of course possible but I thinkt that would be even more
counter-intuitive.

Robert


More information about the erlang-questions mailing list