Professional Documents
Culture Documents
Erlang Part 1
Erlang Part 1
Erlang Part 1
Thanks to Richard Carlsson for the original version of many slides in this part
Erlang buzzwords
z Functional (strict) z Automatic memory
z Single-assignment management (GC)
case Val of
− Value of “Expr” if no
not_found -> exception
%% probably thrown − Value X of “throw(X)”
use_default(X);
{'EXIT', Term} -> for a throw-exception
handle_exit(Term); − “{'EXIT',Term}” for
_ ->
Val
other exceptions
end z Hard to tell what
happened (not safe)
z Mixes up errors/exits
z In lots of old code
Record syntax
-record(foo, z Records are just a
{a = 0 :: integer(),
b :: integer() syntax for working with
| undefined}).
{foo, 0, 1} = #foo{b = 1}
tagged tuples
R = #foo{}
z You don't have to
{foo, 0, undefined} = R remember element
{foo, 0, 2} = R#foo{b=2}
order and tuple size
{foo, 2, 1} = R#foo{b=1, a=2} z Good for internal work
0 = R#foo.a
within a module
undefined = R#foo.b
z Not so good in public
f(#foo{b = undefined}) -> 1;
f(#foo{a = A, b = B})
interfaces (users must
when B > 0 -> A + B; have same definition!)
f(#foo{}) -> 0.
Preprocessor
-include("defs.hrl"). z C-style token-level
preprocessor
-ifndef(PI). − Runs after tokenizing,
-define(PI, 3.1415926).
-endif. but before parsing
z Fully automatic
z Extremely easy to use
z Fast and scalable
z Sound for defect detection
− “Dialyzer is never wrong”
Dialyzer
z Part of the Erlang/OTP distribution since 2007
z Detects
− Definite type errors
− API violations
− Unreachable and dead code
− Opacity violations
− Concurrency errors
Data races (-Wrace_conditions)
z Experimental extensions with
− Stronger type inference: type dependencies
− Detection of message passing errors & deadlocks
How to use Dialyzer
z First build a PLT (needs to be done once)
> dialyzer --build_plt --apps erts kernel stdlib