underscore prefixed variables (was: Re: Noob - Getting Started Infinte Loop?)

Bjorn Gustavsson bjorn@REDACTED
Thu Aug 31 11:19:17 CEST 2006


It is sometimes useful to refer to variables with underscores
in debug code. In the example below, the io:format/2 call can
be simply uncommented when needed:

foo(V) ->
        ....
foo(_V) ->
   %% io:format("~p\n", [_V]),
   error.

/Bjorn

Fredrik Thulin <ft@REDACTED> writes:

> On Thursday 31 August 2006 03:03, Håkan Stenholm wrote:
> ...
> > Note that _V is still a valid variablename, so the code below is
> > valid:
> >
> >     foo(_V) ->
> >         _V * 2.
> >
> >     a call to foo(42) would yield 84 in this case.
> 
> As Håkan points out, _V * 2 is valid. I think this is bad. In 
> combination with assign-once, it introduces bugs like in this example :
> 
>  case lists:keysearch(primary, 1, MyList) of
>     {value, {primary, _Socket}} ->
>         %% do something because our "primary" socket exists, although we 
>         %% don't really care about the socket iself
>         ...
>     false ->
>         case lists:keysearch(secondary, 1, MyList) of
> 	    {value, {secondary, _Socket}} ->
> 	         %% do something because our "secondary" socket exists,
> 	         %% although we don't really care about the socket itself
> 
> I think that
> 
>   a) variables prefixed with "_" should really not be variables that get 
>      set - like the special case _.
>   b) the compiler should issue a warning that you are actually using a
>      variable prefixed with "_".
> 
> Would others care to share their view on this?
> 
> /Fredrik
> 

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list