Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple specifications for same function in Erlang header file

Tags:

erlang

I'm trying to specify a function in a header file. Like so:

-spec update(pid(), tuple(tuple(), integer(), atom()), tuple(atom(), atom())) -> tuple(tuple(), integer(), atom()).

Now I want to add some extra specification for this function because it has multiple different parameters.

update(_Pid, {Specs, infinity, _State}, {Step}) ->
  timer:sleep(10000),
  {{Specs, infinity}, {Step}};
update(_Pid, {Specs, infinity, _State}, {ExtraInfo, Step}) ->
  timer:sleep(10000),
  {{Specs, infinity}, {ExtraInfo, Step}};
update(_Pid, {Specs, N, _State}, _Info) when N < 2 ->
  {Specs, N, stop};
update(_Pid, {Specs, N, _State}, {notTaken, Step}) ->
  {Specs, N, Step};
update(_Pid, {Specs, N, _State}, {taken, Step}) ->
  {Specs, N - 1, Step}.

So I want to add these extra specifications, for the different parameters in this function, in my header file. I don't know how to do this, could someone help me with this?

I tried to do this but it gives me compilation errors:

-spec update(pid(), tuple(tuple(), integer(), atom()), tuple(atom(), atom())) -> tuple(tuple(), integer(), atom()).
-spec update(pid(), tuple(tuple(), atom(), atom()), tuple(integer(), atom())) -> tuple(tuple(), atom(), atom()).
-spec update(pid(), tuple(tuple(), atom(), atom()), tuple(atom())) -> tuple(tuple(), atom(), atom()).

Thanks in advance.

like image 871
Daan Mouha Avatar asked May 26 '15 07:05

Daan Mouha


People also ask

What are the two types of number formats supported by Erlang?

In Erlang there are 2 types of numeric literals which are integers and floats.

What is spec in Erlang?

spec adds up information about the code. It indicates the arity of the function and combined with -type declarations, are helpful for documentation and bug detection tools. Tools like Edoc use these type specifications for building documentation. Tools like Dialyzer use it for static analysis of the code.

How many types of Erlang are there?

7 Types and Function Specifications.


1 Answers

I found the answer here. I had to use the semi-colon ;. I changed this:

-spec update(pid(), tuple(tuple(), integer(), atom()), tuple(atom(), atom())) -> tuple(tuple(), integer(), atom()).
-spec update(pid(), tuple(tuple(), atom(), atom()), tuple(integer(), atom())) -> tuple(tuple(), atom(), atom()).
-spec update(pid(), tuple(tuple(), atom(), atom()), tuple(atom())) -> tuple(tuple(), atom(), atom()).

into this:

-spec update(pid(), tuple(tuple(), integer(), atom()), tuple(atom(), atom())) -> tuple(tuple(), integer(), atom());
            (pid(), tuple(tuple(), atom(), atom()), tuple(integer(), atom())) -> tuple(tuple(), atom(), atom());
            (pid(), tuple(tuple(), atom(), atom()), tuple(atom())) -> tuple(tuple(), atom(), atom()).
like image 65
Daan Mouha Avatar answered Oct 23 '22 03:10

Daan Mouha