Why this erlang prog with type signature can compile?


I have this prog:

-module(a).
-export([add/2]).
-export([add2/1]).

-spec add(integer(),integer())->integer().
add(A,B)->A+B.

add2(C)->C+add(1,"a").

I can compile this prog with no error.but I think I should got error for the line

add(1,"a").

in any static type language,it can't compile,so why erlang will compile this?how to
write the type signature so erlang can catch this error?If erlang can't,can elixir write the same prog but can catch this error?Thanks!

---------------Answer---------------

Erlang is not statically typed language, it is always dynamically typed. You cannot catch it during compilation as erlc do not care about -spec, at all. It is used only for documentation and Dialyzer (which technically is external tool) can use it to do some (limited) static analysis of the contracts.

For how to use Dialyzer check out:

  • Documentation of Dialyzer application
  • Rebar3 Dialyzer task documentation
  • erlang.mk Dialyzer guide

Previous : VBA loop with deleting items
Next : Analyse the action of 'set -- $args'