In Erlang/Elixir variables can be binded only once. This is typical in many functional languages & itβs the way to express immutability in the language. Once we assigned a variable name to a value it cannot be reassigned anymore.
Bad: This does not compile as we try to rebind an already binded variable
a = 1a = 2Good: This compiles as we bind each variable only once
a = 1b = 2So this is the general rule for all variables. There is only one exception: the underscore or any variable that starts with ββ (ie. ββ, β_aβ, β_bβ, etc.). The underscore is the only variable that :
- can be bound multiple times
- cannot be read
The second property is pretty important. If we would be able to read a variable that starts with β_β then these variables would provide a hack around the language immutablity. As we cannot read the variable itβs not a problem that it can be binded multiple times => we still can bind only once the variables that can be read
Ok great. But this seems a pretty strange exception. Why introduce this feature? The short answer is that the underscore exception is there just as a way to tell the compiler βi donβt care about what is thereβ. And this is important because it simplifies several core language features:
- extracting pattern matching
- catch all clauses
- ignore function returns
- specific function signatures
continue reading on remotereason.com
β οΈ This post links to an external website. β οΈ
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.