is it possible to reuse a "-type" specification for a function in a "-spec" ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

is it possible to reuse a "-type" specification for a function in a "-spec" ?

Marco Molteni
Hello,

say I have a function that takes a function as an argument and I want
to write type specifications for them:

   -type decider() :: fun((pos_integer(), #node{}) ->
       reject | accept | continue).

   -spec foo(Decider :: decider()) -> boolean().
   foo(Decider) -> ...

When implementing the callback, I would like to reuse the type decider():

Instead of duplicating the same type information (error prone, can go
out of sync):

   -spec my_cb(pos_integer(), #node{}) -> reject | accept | continue.
   my_cb(X, Tree) -> ...

I would like to write something like:

   -spec my_cb :: decider().
   my_cb(X, Tree) -> ...

Is there a way?


thanks!
marco

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to reuse a "-type" specification for a function in a "-spec" ?

Kostis Sagonas-2
On 07/20/2016 10:21 PM, Marco Molteni wrote:

> Hello,
>
> say I have a function that takes a function as an argument and I want
> to write type specifications for them:
>
>    -type decider() :: fun((pos_integer(), #node{}) ->
>        reject | accept | continue).
>
>    -spec foo(Decider :: decider()) -> boolean().
>    foo(Decider) -> ...
>
> When implementing the callback, I would like to reuse the type decider():
>
> Instead of duplicating the same type information (error prone, can go
> out of sync):
>
>    -spec my_cb(pos_integer(), #node{}) -> reject | accept | continue.
>    my_cb(X, Tree) -> ...
>
> I would like to write something like:
>
>    -spec my_cb :: decider().
>    my_cb(X, Tree) -> ...
>
> Is there a way?

Yes, an ugly one, using macros.

I would not recommend using that option though.

Kostis

PS. Some of the duplication in your example, can be avoided in a nice
way by giving names to types.  For example, you can define:

-type node_rec()    :: #node{}.
-type decider_ret() :: reject | accept | continue.
-type decider_fun() :: fun((pos_integer(),node_rec()) -> decider_ret()).
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: is it possible to reuse a "-type" specification for a function in a "-spec" ?

Marco Molteni

> On 28 Jul 2016, at 07:25, Kostis Sagonas <[hidden email]> wrote:
>
> On 07/20/2016 10:21 PM, Marco Molteni wrote:

[..]

>> I would like to write something like:
>>
>>   -spec my_cb :: decider().
>>   my_cb(X, Tree) -> ...
>>
>> Is there a way?
>
> Yes, an ugly one, using macros.
>
> I would not recommend using that option though.

Ah, I see, and I see why you don't recommend it...

> Kostis
>
> PS. Some of the duplication in your example, can be avoided in a nice way by giving names to types.  For example, you can define:
>
> -type node_rec()    :: #node{}.
> -type decider_ret() :: reject | accept | continue.
> -type decider_fun() :: fun((pos_integer(),node_rec()) -> decider_ret()).

I though of something along these lines. So this is the best that can be done, OK.

thanks for your answer!

marco.m
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions