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

Zoltan Lajos Kis kiszl@REDACTED
Tue Jan 5 09:16:01 CET 2010


> 2010/1/4 Zoltan Lajos Kis <kiszl@REDACTED>
>
>> 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
>

I understand that they are ordinary variables. But I am certain that all
the _Var variables I have seen so far were named so in order to indicate
that they are intentionally unused, and not because of some strange naming
habit.

I also understand that many compiler warnings (unused variable, wrong no.
of args in format call, etc.) are output because there is a clear sign of
a (semantic) mistake and not because of the source code being invalid.
So I believe that the current behavior of not warning about a most
probable mistake with _Var variables is the counter-intuitive one.

Anyway, such a change would only have a negative effect on those who
purposefully use such variable names for "ordinary variables". Would not
affect those who only use the anonymous variable, and would help those
using these variables as I described.

Zoltan.



More information about the erlang-questions mailing list