Compiler Diagnostics

When we compile a program, the compiler provides us with helpful error messages if our source code is syntactically incorrect. Most of these are self-evident: if we omit a bracket, a comma, or a keyword, the compiler will give an error message with the filename and line number of the offending statement. The following are some errors we could see.

Head Mismatch

We’ll get the following error if the clauses that make up a function definition do not have the same name and arity:

bad.erl
Line 1 
foo(1,2) ->
a;
foo(2,3,a) ->
b.
 
1>​ c(bad).
 
./bad.erl:3: head mismatch

Unbound Variables

Here’s some code containing unbound variables:

bad.erl
Line 1 
foo(A, B) ->
bar(A, dothis(X), B),
baz(Y, X).
 
1>​ c(bad).
 
./bad.erl:2: ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.