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?

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.

7 Likes