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

Zoltan Lajos Kis kiszl@REDACTED
Tue Jan 5 10:01:13 CET 2010


> Zoltan Lajos Kis wrote:
>>>> 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.
>
> Well, your statement depends on the code that you've seen, but my
> experience is different.  We often use the following coding convention.
>   We define a macro that outputs some debugging information:
>
> -ifdef(DEBUG).
> -define(debug(Str, Vars),  io:format(Str, Vars)).
> -else.
> -define(debug(Str, Vars),  ok).
> -endif.
>
> and use it in code that looks as follows:
>
> foo(....) ->
>      case do_something(...) of
>        {X,Y,Z} -> ...
>        _Other ->
>           ?debug("Strange: do_something returned ~p\n", [_Other]),
>           ....
>      end.
>
> We want the compiler to be silent about _Other being unused when not
> debugging and use _Other as an ordinary variable when debugging is on.
> We definitely do not want the compiler to be warning for used
> underscore-starting variables in such cases.
>
> Kostis
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>

You can change that to this, and you will get no warnings:

-ifdef(DEBUG).
-define(debug(Str, Vars),  io:format(Str, Vars)).
-else.
-define(debug(Str, Vars),  Vars=Vars).
-endif.

Zoltan.



More information about the erlang-questions mailing list