Fl4m3Ph03n1x

Fl4m3Ph03n1x

Multiple arity function with spec

Code

Let’s say I have a function that returns a map. This function can have 0 and 1 arity:

  @spec new_game(String.t) :: map
  def new_game(a_word), do:
    %{word: a_word}

  @spec new_game :: map
  def new_game, do:
    new_game(Dictionary.random_word())

Now I am trying to spec this function. However, IIRC, since I have 2 different specs, dialyzer will override the first spec with the last one (could someone correct me here? I am fairly sure this happens in some occasions).

Questions

  1. Is it possible to have only one spec definition that covers both cases of this function?
  2. Are there any drawbacks in have multiple specs per function?

Marked As Solved

LostKobrakai

LostKobrakai

Remember that for the beam both those functions are different functions, because the arity is different, so there’s nothing removed or anything. It gets more tricky for multiple function heads of the same function (and arity), where dialyzer afaik effectively uses a union of all available specs.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics 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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement