[erlang-questions] Underscore prefixes

Joe Armstrong erlang@REDACTED
Fri Apr 18 15:49:38 CEST 2008


_X behaves just like X as regards pattern matching. It is not an
anonymous variable like "_"

Thus {_X, _X} matches {a,a} but not {a,b}

The difference between X and _X shows up in the compiler diagnostics
for unused variables.
If X occurs only in a pattern and is never used, then the compiler
issues a warning.

If _X is used instead, then no warning is issued.

Thus in Dan's code if the io:format line is commented out there will
be no compiler warning. If the program
used Result instead of _Result then there would be a warning.

So use "_Var" in debug statements that are intended to be commented out.

/Joe Armstrong

On Fri, Apr 18, 2008 at 8:31 AM, Dan Gudmundsson <dgud@REDACTED> wrote:
> My guess is debugging:
>
>    _Result = module:function(X),
>   io:format("DBG: ~p ~n", [_Result]),
>   {response, ok}.
>
>  Than you can delete the debug io:format.
>
>
>
>  Matthew Dempsky wrote:
>  > On Thu, Apr 17, 2008 at 7:13 PM, Vik Olliver <vik@REDACTED> wrote:
>  >> Underscores before a variable indicate it's basically optional, right?
>  >
>  > It simply tells the compiler not to issue a warning if the variable is
>  > not otherwise used.
>  >
>  >>  So why would someone do:
>  >>
>  >>  _Result = module:function(X),
>  >>  {response, ok}.
>  >>
>  >>  in their code?
>  >
>  > Style maybe.  When matching elements of a tuple, I'll often still
>  > write "_Foo" and "_Bar" (but actually meaningful names) even if I
>  > don't need those elements in case I later need to change the code and
>  > it saves me a little bit of time looking up what the other elements
>  > are again.
>  >
>  > This particular case though, it's kinda silly.  Obviously the return
>  > value of a function is its result. :-)
>  > _______________________________________________
>  > erlang-questions mailing list
>  > erlang-questions@REDACTED
>  > http://www.erlang.org/mailman/listinfo/erlang-questions
>  >
>  _______________________________________________
>  erlang-questions mailing list
>  erlang-questions@REDACTED
>  http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list