[erlang-questions] Erlang Type specifications and throw

Jesse Gumm <>
Tue Apr 2 09:14:32 CEST 2013


(including the ML with this one)

Hi Rudolph,

With a situation like this, the more canonical Erlang approach is to do
something like: on success, return {ok, Pid} and on failure return the atom
'error'.

Then the spec would simply be:

-spec where(G::atom()) -> {ok, pid()} | error.

Then you can trivially use it in a case statement and use pattern matching.

Using a combination of throw and try blocks as intended behavior for
control flow is a recipe for confusion.

-Jesse
On Apr 2, 2013 12:22 AM, "Rudolph van Graan" <> wrote:

> Hi,
>
> I want to write a type spec for a function that throws exceptions,
> something like this:
>
> -spec where(G :: atom()) -> pid() | throw({no_name,atom()}).
>
>
> I don't want to use an exit signal, because this behaviour is intentional
> and the calling process will catch this and take specific action.  But I
> can't write this using the -spec syntax. The documentation says:
>
> Some functions in Erlang are not meant to return; either because they
> define servers or because they are used to throw exceptions as the function
> below:
>
>   my_error(Err) -> erlang:throw({error, Err}).
>
> For such functions we recommend the use of the special no_return() type
> for their "return", via a contract of the form:
>
>   -spec my_error(term()) -> no_return().
>
>
> no_return() is completely the wrong thing to use and just leaving out the
> throw(…) declaration is also wrong in my eyes. How do I do this?
>
> Thanks,
>
> Rudolph van Graan
>
>
>
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130402/c29a2c76/attachment.html>


More information about the erlang-questions mailing list