IwoHerka

IwoHerka

Should I provide typespec for each function clause?

When creating multiple clauses for a function, should I provide typespec for each one? Example:

@spec foo([]) :: {:error, any}
def foo([]) do
  ...
  {:error, ...}
end

@spec foo([integer]) :: {:ok, any}
def foo(some_list) do
  ...
  {:ok, ...}
end

…or is something like this preferrable?


@spec foo([integer]) :: {:ok, any} | {:error, any}

def foo([]) do
  ...
  {:error, ...}
end

def foo(some_list) do
  ...
  {:ok, ...}
end

Thanks!

First Post!

LostKobrakai

LostKobrakai

Both work, internally there’s no difference and the multiple specs will become unions of all separate specs anyways.

Most Liked

rvirding

rvirding

Creator of Erlang

You are defining ONE function so it should be for that function which you give the typespec.

sbuttgereit

sbuttgereit

I’ll typically use the second approach.

The different function heads which ultimately need to be defined by the spec are typically grouped together and having a spec at the top of that group makes for a reasonable signpost in the source code for the beginning of that group.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement