It appears that when you define the behavior, you need to include type specifications in the @callback definition. Then, when you accept the behavior, the compiler requires that function_name/arity be defined, but is completely happy if you do not follow type specifications.
My questions:
- Are these observations correct?
- If so, why are the
@callback specifications a @callback type with the actual functionality of checking that function_name/arity ? This makes it difficult to understand what documentation is and what is the core functionality. The rest of the elixir seems to explicitly separate the two, preserving type specifications as an optional addition.
For instance:
If we omit the type specifications, we get a compilation error
defmodule Greeting do @callback hello(person) end
To make the compiler happy, we must include type specifications:
defmodule Greeting do @callback hello(%Person{}) :: {:ok, String.t} | {:error, String.t} end
Now that we accept the behavior, the compiler checks that function_name/arity defined:
defmodule WesternGreeting do @behaviour Greeting def hello(), do: "Howdy" end
However, all type specifications in @callback are not taken into account by the compiler:
defmodule WesternGreeting2 do @behaviour Greeting def hello([a, b, c]), do: a <> b <> c end
source share