Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type of non-terminating function in Erlang

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?

like image 852
ppopoff Avatar asked May 12 '16 10:05

ppopoff


People also ask

How many types of Erlang are there?

7 Types and Function Specifications.

How do you define a function in Erlang?

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.

Is Erlang typed?

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).

How do I use Erlang dialyzer?

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.


1 Answers

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().
like image 132
legoscia Avatar answered Sep 23 '22 14:09

legoscia