[erlang-questions] leading underscores on variables versus _

Ulf Wiger (TN/EAB) ulf.wiger@REDACTED
Fri Feb 2 15:55:39 CET 2007


I have on occasion used underscored variables
in the following way

foo([{x, _, _, Thingy, _} = _Obj| T]) ->
   ?dbg("_Obj = ~p~n", [_Obj]),
   ...

The great and marvellous advantage is of course 
that if I redefine dbg to expand to a constant,
the linter doesn't start warning about unused 
variables.

(So bizarre code like yours is not the only code
that would break, but also bizarre code like mine.) 

BR,
Ulf W

> -----Original Message-----
> From: erlang-questions-bounces@REDACTED 
> [mailto:erlang-questions-bounces@REDACTED] On Behalf Of 
> Matthias Lang
> Sent: den 2 februari 2007 15:14
> To: erlang-questions@REDACTED
> Subject: Re: [erlang-questions] leading underscores on 
> variables versus _
> 
> Ulf Wiger writes:
> 
>  > Underscores are always ignored. The variables that begin  
> > with an underscore are actually real variables. The  > 
> linter interprets them as "don't care" patterns, but the  > 
> compiler does not.
> 
> This seems to catch many or even most beginners. It seems 
> natural (?) to expect _name to be a longer way of writing _, 
> i.e. that any variable starting with _ means exactly the same 
> thing as if you'd just written _.
> 
> An example where that doesn't hold:
> 
>   f(_x, _x) -> same;
>   f(_,_) -> different.
> 
> Changing _x to behave as many (most?) initially expect would 
> break backwards compatibility, but only for misleading code 
> such as the above...
> 
> Matthias
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
> 




More information about the erlang-questions mailing list