[erlang-questions] type specification of functions with same signature
Karlo Kuna
kuna.prime@REDACTED
Sun Oct 23 09:58:47 CEST 2016
sure macro would work fine:
-define(MY_F_T, (list(),atom())-> boolean()).
-define(MY_F_T(Name), -spec Name?MY_F_T).
?MY_F_T(foo).
-spec hfoo(F) -> ok when F :: fun(?MY_F_T).
also this could be made more general by adding:
-define(FUN_SPEC(Name, Spec), -spec Name Spec).
?FUN_SPEC(foo,?MY_F_T).
but this is starting to be ugly and unreadable
in general i strongly dislike macros in any language but ...
it would bi nice if this could be done through spec like:
-spec foo :: my_f().
this is clean
but thanks for help i will try to see how solutions with macros works in my
code base until some other solution comes by.
On Sun, Oct 23, 2016 at 1:01 AM, Kenneth Lakin <kennethlakin@REDACTED>
wrote:
> On 10/22/2016 03:43 PM, Karlo Kuna wrote:
> > sure dialyzer will report error but callback functions are just one
> > motivation, for me the first example is far more important
> > as it reduces spec duplication which is annoying and not very expressive
> > IMHO.
>
> Oh, drat. Right. You want to be able to vary the names of the function.
> You probably already thought about this, but how about a macro?
>
> -define(MY_F_SPEC(Fun), -spec Fun(list(),atom())->boolean()).
>
> ?MY_F_SPEC(foo).
> foo(L, A) -> true.
>
> ?MY_F_SPEC(bar).
> bar(L, A) -> false.
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20161023/10739db4/attachment.htm>
More information about the erlang-questions
mailing list