Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous variables in Erlang

What are the exact differences between underscore variables and a named variable that starts with underscore from the Erlang compiler point of view (apart from adding readability to the code)?

For example are _ and _Var different?

like image 919
coffeMug Avatar asked Dec 04 '12 16:12

coffeMug


People also ask

What are anonymous variables?

The anonymous variable is an element of Prolog that is similar to a variable in almost every way. As presumable, all the occurrences of the name of an ordinary variable stand for the same variable within one clause. On the contrary, every occurrence of _denotes a distinct variable .

What is fun () in Erlang?

Advertisements. Funs are used to define anonymous functions in Erlang.

What is pattern matching in Erlang?

Pattern matching occurs when evaluating a function call, case- receive- try- expressions and match operator (=) expressions. In a pattern matching, a left-hand side pattern is matched against a right-hand side term. If the matching succeeds, any unbound variables in the pattern become bound.


2 Answers

The don't care variable _ is a VERY SPECIAL variable which matches anything and is NEVER bound to a value. It is used when I know there is something there but I don't care what the value is and I will never use. Seeing _ is never bound it can not be used in an expression and the compiler flags it as an error.

Variables like _Var are perfectly normal variables which you can match against and will be bound to values which means they can be used in expressions. Prefixing a variable with _ is about intent. The compiler normally warns you about a variable which is bound in a pattern but is never used, often a sign of an error. But the compiler does not warn for variables prefixed with _ like in _Var. The intent being that I want to give the variable a name, naming things is good, but that I know I will never use it.

Remember that _ is really the only special variable and that _Var are normal variables and behave as such if used. If you are feeling perverse then you could prefix all your variables with _ and everything will still work.

like image 58
rvirding Avatar answered Oct 10 '22 15:10

rvirding


Let's quote the doc here:

The anonymous variable is denoted by underscore (_) and can be used when a variable is required but its value can be ignored. [...]

Variables starting with underscore (_), for example _Height, are normal variables, not anonymous: they are however ignored by the compiler in the sense that they will not generate any warnings for unused variables.

In other words, you use _Var form when you need the matched expression to be matched - but don't want to use it further AND/OR you want show its meaning. And you use _ variable when neither you nor compiler should care for the expression that will be matched by it.

Example 1:

member(_, []) -> [].

In this function is not quite clear what the first _ matches. But rewriting it directly, like this:

member(Elem, []) -> [].

... will generate a warning, if the code is compiled with the flag warn_unused_vars set. You still can make you code readable here, though, by using underscored variable:

member(_Elem, []) -> [].

Example 2:

{_, _, Some} = {1, 2, 3}

This tuple matching will go though quite all right, as the first two elements of tuple will be ignored completely.

{_Var, _Var, Some} = {1, 2, 3}

This matching will fail, however: though _Var won't have to be used, it should be 'filled' with the same value! As 1 is not equal to 2, the condition fails here.

like image 35
raina77ow Avatar answered Oct 10 '22 17:10

raina77ow