Elixir type spec for a function with default parameters

It works as expected because you actually define two functions.

@spec foo(integer) :: integer
def foo(bar \\ 10) do
  bar
end

Is equivalent to:

def foo() do
  foo(10)
end

@spec foo(integer) :: integer
def foo(bar) do
  bar
end

So you basically end up with two functions but only one of them has @spec.


Yes.

I would add that if your question is if there is a difference between the typespec of a function which has an argument with a default value and an argument that doesn't, then no there is no difference.

Tags:

Elixir