[erlang-questions] Compiler to warn on used underscore-variables
Zoltan Lajos Kis
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
>> The "standard" way to suppress this warning is to put an underscore in
>> of the variable name.
>> As a result, an underscore-variable in source codes gives us (at least
>> 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
>> 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
> 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
> 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
> are never used. It was included to allow users to to give names to
> they don't intend to use for documentation purposes. I personally don't
> 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
>> unused) in the code ?
> It is of course possible but I thinkt that would be even more
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
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.
More information about the erlang-questions