Anonymous variables in Erlang

erlang, variables

Solution

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.

Problem

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?

Original source