September 2013
Intermediate to advanced
548 pages
12h 25m
English
Let’s look at what happens when we add type specifications to
code. We’ll start with the well-known boolean and
function. and is true if both its arguments are
true, and it is false if any of its arguments are
false.
We’ll define a function myand1 (which
is supposed to work like and) as follows:
| types1.erl | |
| | myand1(true, true) -> true; |
| | myand1(false, _) -> false; |
| | myand1(_, false) -> false. |
Running typer on this, we see the following:
| | $typer types1.erl |
| | -spec myand1(_,_) -> boolean(). |
| | ... |
The inferred type of myand1 is (_,_) ->
boolean(), which means that each of the arguments to
myand1 can be anything you like, and the return type will
be boolean. It infers that the arguments to myand1 can be anything because ...
Read now
Unlock full access