What is happening when an Elixir module defines `@callback` and also implements the specified function

Viewed 113

For example:

defmodule Demo.Validators.AddressValidator do
  @callback validation_level() :: String.t
  @callback set_validation_level(String.t) :: :ok
  @callback validate(String.t) :: boolean

  def validation_level() do
    Application.get_env(:demo, :validation_level)
  end

  def set_validation_level(level) do
    Application.put_env(:demo, :validation_level, level)
  end

  def validate(address) do
    emit_event("address_validated")

    address
    |> String.length()
    |> validate_length()
  end

  defp validate_length(address_length) when address_length < 32, do: true
  defp validate_length(_address_length), do: false

  defp emit_event(event_description), do: Demo.Events.EventEmitter.emit(event_description)
end

Taken from here

I can see examples all over the web where a module defines a @callback and then another module implements the functions using @behaviour and @impl, but I'm not clear on what's happening when the @callback and the implementation are in the same module. What's the intention?

0 Answers
Related