Variables and single assignment

Contents
Bind once
No rebinding
The underscore
Where variables live

Bind once

Variables start with an uppercase letter or an underscore. The equals sign binds a name to a value, and the name then stands for that value everywhere in its scope.

1> Name = "Ada".

"Ada"

2> Answer = 42.

42

1> Name = "Ada".

"Ada"

2> Answer = 42.

42

No rebinding

Single assignment means a bound name never changes. The equals sign is a match: it checks that the right side equals the stored value, and fails loudly when it does not.

This looks strict at first and pays off in concurrent code, where two processes can never race over a mutable cell.

1> Answer = 42.

42

2> Answer = 42. % ok: the same value

42

3> Answer = 43.

** exception error: no match of right hand side value 43

1> Answer = 42.

2> Answer = 42. % ok: the same value

3> Answer = 43.

** exception error: no match of right hand side value 43

The underscore

The lone underscore matches anything and binds nothing. It says: I do not care about this part.

An underscore with a name, like _Name, binds the value but warns that you mean to ignore it.

1> {ok, _} = {ok, 7}.

{ok,7}

2> {error, _Reason} = {error, enoent}.

{error,enoent}

1> {ok, _} = {ok, 7}.

{ok,7}

2> {error, _Reason} = {error, enoent}.

{error,enoent}

Where variables live

A binding lives in the function clause that made it. Another clause of the same function starts with a clean slate, even when it runs the same name.

The shell is one big clause, which is why a name typed once stays bound until you restart erl.

Next: Numbers

Article author: Arthur Isaev

Related articles
Erlang
Erlang course: zero to hero in 50 lessons
What is Erlang and where it runs
Install Erlang and check the version
erl: the interactive shell
Your first Erlang script
Variables and single assignment
Numbers
Atoms
Strings and binaries
Truthiness: only false and nil
Conditions: case and if
Recursion replaces loops
Lists
Tuples
Maps
Functions and clauses
Pattern matching
Guards
Records
Exceptions: try and catch
Modules, exports and imports
Funs: anonymous functions
Higher-order functions
Keyword lists
Erlang idioms and clean code
Rebar3 builds
Testing with EUnit
Debugging
Encodings and Unicode
Time and date
JSON and term formats
Processes and message passing
Spawn and links
Monitors and exits
GenServer
Supervisors
OTP applications
Files and IO
Sockets
HTTP clients
io:format and output
Let it crash: error handling
ETS tables
Timers and timeouts
Code loading
Distributed Erlang
Capstone: todo CLI
Capstone: TCP server
Performance
Modern Erlang tour
Hero roadmap: the whole course on one page
Install Erlang on Windows 11
Install Erlang on Ubuntu
Install Erlang on Rocky Linux
Install Erlang on macOS
Install Erlang on FreeBSD
Erlang in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: