[erlang-questions] Fun type specifications

37 views
Skip to first unread message

Loïc Hoguin

unread,
Sep 17, 2012, 7:20:12 AM9/17/12
to erlang-questions
Hey,

I came across this limitation a few times now. Assuming this is a
limitation because I didn't find a solution.

For function specs you can do something like this:

-spec my_fun(Req) -> Req when Req::req().

You can't do this however.

-type my_fun_type() -> fun((Req) -> Req when Req::req()).

This is a bit annoying as this makes the typing of the funs a little
less precise than I hoped.

Did I miss something and can you still do something like this? Or if
not, can this be added eventually?

Thanks.

--
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Francesco Mazzoli

unread,
Sep 17, 2012, 7:33:20 AM9/17/12
to erlang-questions
At Mon, 17 Sep 2012 13:20:12 +0200,

Loïc Hoguin wrote:
> -type my_fun_type() -> fun((Req) -> Req when Req::req()).
>
> This is a bit annoying as this makes the typing of the funs a little
> less precise than I hoped.
>
> Did I miss something and can you still do something like this? Or if
> not, can this be added eventually?

Well, can't you simply do

-type my_fun_type() -> fun((req()) -> req()) | other_type.

?

--
Francesco * Often in error, never in doubt

Loïc Hoguin

unread,
Sep 17, 2012, 7:33:38 AM9/17/12
to erlang-q...@erlang.org
On 09/17/2012 01:33 PM, Francesco Mazzoli wrote:
> At Mon, 17 Sep 2012 13:20:12 +0200,
> Loïc Hoguin wrote:
>> -type my_fun_type() -> fun((Req) -> Req when Req::req()).
>>
>> This is a bit annoying as this makes the typing of the funs a little
>> less precise than I hoped.
>>
>> Did I miss something and can you still do something like this? Or if
>> not, can this be added eventually?
>
> Well, can't you simply do
>
> -type my_fun_type() -> fun((req()) -> req()) | other_type.

This doesn't show the relationship between the input and output, and I
get many warnings in one case if I do that (probably due to req() being
an opaque type).

--
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu

Francesco Mazzoli

unread,
Sep 17, 2012, 7:43:47 AM9/17/12
to erlang-q...@erlang.org
At Mon, 17 Sep 2012 13:33:38 +0200,

Loïc Hoguin wrote:
> This doesn't show the relationship between the input and output

It definitely shows that the return type and the argument's type are the same.

> and I get many warnings in one case if I do that (probably due to req() being
> an opaque type).

That is weird, which warnings? Do you mean when type checking your code or for
the declaration? I would expect the two declaration to be equivalent, but maybe
there are subtleties.

--
Francesco * Often in error, never in doubt

Reply all
Reply to author
Forward
0 new messages