Introduction

Comments to the book "Learn You Some Erlang For Great Good" by Fred Hébert, Chapter 4 - Types (or lack thereof). Pattern matching is much easier to achieve in Erlang than in many other languages, but the syntax takes some time to get used to. Instead of if-else or similar conditional branching, in Erlang you simply use functional declarations with a pattern. Learning goals: Understand how Erlang's "crash first" philosophy and always accounting for errors works with dynamical typing to keep things safe. Challenges: See how the lack of static types does not mean that Erlang code is error prone.

Introduction

This chapter is about how Erlang handles types. It is easy or difficult, depending on if you choose to get into static types or not. Here I won be getting into static types in Erlang, because it is too complex at this stage and it is also not needed to make perfectly functional Erlang programs. Static types, Dialyzer and typer will be added as separate chapters in the Advanced-section on this site. Stay tuned. Note that the static types-part of the chapter in Learn You Some Erlang For Great Good is outdated and partially wrong (because of updates in R13B04 onwards).

Dynamic and Strong Typing

Erlang is both dynamically typed and strongly typed. Dynamically typed means that the type is inferred by Erlang; there is no need to specify the type. Many programmers see static typing as an important part of code safety. However, Erlang has it own way of ensuring this, through a "crash first" and "program for what you know" philosophy:

Erlang uses a strategy where it is assumed that errors will happen anyway and makes sure to cover these cases: Erlang's dynamic type system is not a barrier to reliability and safety of programs.

Strongly typed means that Erlang does not do implicit type conversion between terms; it will throw an error instead. You need to explicitly state all type conversions. Examples:

1> X = 10 + "1" .
** exception error: an error occurred when evaluating an arithmetic expression
     in operator  +/2
        called as 10 + "1"

2> Y = 10 + erlang:list_to_integer("1") .
11

In the last example, we use an explicit type conversion list_to_integer. Type conversions are done by so-called built-in functions (BIFs) in the erlang module. All the conversion BIFs are (as per ERTS Reference Manual V.8.1):

Conversion BIFs in the Erlang module
atom_to_binary/2 atom_to_list/1 binary_to_atom/2 binary_to_existing_atom/2
binary_to_float/1 binary_to_integer/1 binary_to_integer/2 binary_to_list/1
binary_to_list/3 binary_to_term/1 binary_to_term/2 * bitstring_to_list/1
float_to_binary/1 float_to_binary/2 float_to_list/1 float_to_list/2
fun_to_list/1 integer_to_binary/1 integer_to_binary/2 integer_to_list/1
integer_to_list/2 iolist_to_binary/1 list_to_atom/1 list_to_binary/1
list_to_bitstring/1 list_to_existing_atom/1 list_to_float/1 list_to_integer/1
list_to_integer/2 list_to_pid/1 list_to_tuple/1 pid_to_list/1 **
port_to_list/1 ** term_to_binary/1 term_to_binary/2 tuple_to_list/

* The second argument is an options list. Passing in [safe] will ensure that the binary won't be decoded if it contains unknown atoms or anonymous functions.

** Should be used for testing purposes only.

Guards and Types

We have already looked at guards and how they can be used when pure pattern matching falls short, for instance, when using ranges. So how do we use guards to test for a given type? We use type test BIFs. All the type test BIFs that can be used in guard expressions are (as per ERTS Reference Manual V.8.1):

Type test BIFs that can be used in guards
is_atom/1 is_binary/1 is_bitstring/1 is_boolean/1
is_builtin/3 is_float/1 is_function/1 is_function/2
is_integer/1 is_list/1 is_map/1 is_number/1
is_pid/1 is_port/1 is_record/2 * is_record/3 **
is_reference/1 is_tuple/1

* Allowed in guard tests, if RecordTag is a literal atom.

** Allowed in guard tests if RecordTag is a literal atom and Size is a literal integer.

Note that is_alive/1, is_process_alive/1 are not type tests, nor are they allowed in guards.

References

  • Learn You Some Erlang - Types (or lack thereof) (Chapters in the paper version of the book: 4.Types (or Lack Thereof), p.55)
  • Programming Erlang, 9 - Types
  • Erlang Reference Manual, Erlang Types

Other Erlang Resources

Sites

The Erlang main site

Erlang Reference Manual User's Guide

Erldocs - An alternative to the official sites.

Erlang Patterns - A collection of Erlang patterns

Rebar3 - A build tool for Erlang that makes it easy to compile and test Erlang applications and releases.

Communities

Erlang mailing lists and forums

The Google group Erlang Programming

Erlang on Stack Exchange

Erlang on Freenode - Use #Erlang

Erlang on Slack

Books

"Learn You Some Erlang For Great Good", by Fred Hebert

"Programming Erlang", by Joe Armstrong

Articles

The Zen of Erlang, by Fred Hebert. A partly practical, partly philosophical take on Erlang.

Other

Ericsson's coding standard for Erlang - Programming rules and conventions.

Getting started with Erlang using IntelliJ IDEA (including Rebar3).

Comments (0)

There are no comments posted here yet

Leave your comments

Posting comment as a guest. Sign up or login to your account.
0 Characters
Attachments (0 / 3)
Share Your Location
Eric Eikrem posted a comment in Erlang 3: Syntax in Functions
You are right! Fixed. Thanks for heads-up
mammon88 posted a comment in Erlang 3: Syntax in Functions
You say that andalso, orelse can be nested inside guards. Example pls.
The Penguin posted a comment in Erlang 3: Syntax in Functions
Seems something's missing after "Running the code gives us:" ...
Erlanger posted a comment in Erlang 2: Modules
These Erlang articles are the same as those on https://erlangexplained.com? Good job, btw... ...
Eric Eikrem posted a comment in Two-Host XenServer Pool With Manual Fail-Over
Your question is a little short on detail, @KenoZ. If you could provide error messages and details o...