[erlang-questions] Typespecs of functions that may fail

Stavros Aronis <>
Tue Jan 17 16:08:28 CET 2017


Hi Bastien,

can it be the case that "is_relation/2" is only used "locally" (i.e. within
the module itself) and all such calls to "is_relation/2" will return
'false'?

This looks like a case where dialyzer has inferred such a thing and thus
complains about the pattern that can't match.

Regards,

Stavros

On Tue, Jan 17, 2017 at 3:47 PM, Bastien Chamagne <>
wrote:

> Hello,
>
> 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?
>
> Cheers!
>
> *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
> archive.*
>
>
> _______________________________________________
> 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/20170117/c5873f0c/attachment.html>


More information about the erlang-questions mailing list