[erlang-questions] Typespecs of functions that may fail

Bastien Chamagne bchamagne@REDACTED
Tue Jan 17 15:47:06 CET 2017


I'm trying to use dialyzer and I don't know how to type functions that 
are written in "happy path" (may crash).
Here's a sample function that may or may not crash depending if Field 
correspond to a relation field on given Model:

     -spec is_relation(field(), model()) -> {true, relation_type()} | false.

     %% @doc Sets a relation on the given model.
     -spec set_relation(field(), model(), model()) -> model() | no_return().
     set_relation(Field, RelatedModel, Model) ->
         % On next line, I'm OK that this function crashes if I try to 
set a relation on a non-relation field
         {true, RelationType} = is_relation(Field, Model),
         set_relation(Field, RelatedModel, Model, RelationType).

Here's what dialyzer says:

     model.erl:180: Function set_relation/3 has no local return
     model.erl:181: The pattern {'true', RelationType} can never match 
the type 'false'

Is there anything that I can do to inform dialyzer that this is a OK 
behaviour for me?


/ps: I'm using erlang 17 if that matters./
/ps: Sorry if it's a duplicate, I can't find the previous message on the 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170117/c360c998/attachment.htm>

More information about the erlang-questions mailing list