delegate_behaviour

Elixir macros to define modules that delegate to concrete implementations of behaviours. Useful to separate "logic to select specific implementation of behaviour" and "caller of behaviour interface" (similar to dependency injection).

Hex.pmHex.pmBuild StatusCoverage StatusGithub IssuesPending Pull-Requests

Installation

Usage example

Suppose you have a behaviour and two modules that implement the behaviour.

iex> defmodule B do
...>   use Behaviour
...>   @callback f(integer) :: integer
...> end
iex> defmodule Impl1 do
...>   @behaviour B
...>   def f(i), do: i + 1
...> end
iex> defmodule Impl2 do
...>   @behaviour B
...>   def f(i), do: i + 2
...> end

Now you can easily define a module that implements the behaviour B by delegating all behaviour functions to the module Impl1 or Impl2. There are two variants of delegations:

All behaviour interface functions are generated as delegations, together with their type specifications.