[erlang-questions] Overloaded function specifications as types

Alex S. alex0player@REDACTED
Mon Jan 23 11:38:29 CET 2017


> 23 янв. 2017 г., в 13:30, Dániel Szoboszlay <dszoboszlay@REDACTED> написал(а):
> 
> A function's type specification can be overloaded:
> 
> -spec foo(integer()) -> integer()
>        ; (atom()) -> atom().
> 
> Is there a way I can use this overloaded function's type in non-spec contexts? Like I want to say bar/1 takes a function as an argument that has a spec like foo/1 above.
> 
> I can think of the following options, but neither of them is exactly what I want:
> 
> -spec bar(fun((integer() | atom()) -> integer() | atom())) -> ok.
> %% Allows a function taking an integer and returning an atom.
> 
> -spec bar(fun((integer()) -> integer()) | fun((atom()) -> atom())) -> ok.
> %% Allows fun erlang:atom_to_list/1, which doesn't work on integers at all.
Unfortunately, that requires having contravariant first-class types, which Dialyzer doesn’t support by design currently.
Best thing you can do in your case is writing fun((X) -> X) when X :: atom().
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20170123/04d54f0b/attachment.htm>


More information about the erlang-questions mailing list