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

Fredrik Thulin ft@REDACTED
Thu Aug 31 10:57:34 CEST 2006


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



More information about the erlang-questions mailing list