I'm learning Erlang and trying to use Dialyzer to get maximum type-safety when it's possible. There's a thing that I don't understand: what is the type of non-terminating function and how to denote it in -spec
. Could anyone shed some light on this?
7 Types and Function Specifications.
A function is uniquely defined by the module name, function name, and arity. That is, two functions with the same name and in the same module, but with different arities are two different functions. A function named f in the module m and with arity N is often denoted as m:f/N.
Erlang is a dynamically and strongly typed language that is famous for allowing teams to reduce the lines of codes needed in their system. As a result, the teams required for many Erlang systems have been small (just think of Whatsapp managing 900 million users with a backend team of only 50 server-side developers).
Use Dialyzer from the command line to detect defects in the specified files or directories containing . erl or . beam files, depending on the type of the analysis. Display the full path names of files for which warnings are emitted.
A function that loops forever and never terminates has the return type no_return()
. (That return type is also used for functions that always throw exceptions, e.g. a custom error function. If you don't specify that return type, Dialyzer will tell you that the function "has no local return".)
This is mentioned in the Types and Function Specifications chapter of the Erlang Reference Manual:
Some functions in Erlang are not meant to return; either because they define servers or because they are used to throw exceptions, as in the following function:
my_error(Err) -> erlang:throw({error, Err}).
For such functions, it is recommended to use the special
no_return()
type for their "return", through a contract of the following form:-spec my_error(term()) -> no_return().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With