typeserlangdialyzer

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?


Solution

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