/mixin

🤔Delegates all functions to ModuleA from moduleB. Works like a so-called Mixin.

Primary LanguageElixir

hex.pm version CI GitHub code size in bytes

Mixin

Mixin for Elixir. Delegates all functions to ModuleA from moduleB. Works like a so-called Mixin.

Installation

def deps do
  [
    {:mixin, "~> 0.1.0"}
  ]
end

Usage

iex> defmodele ForDocFoo do
...>   def foo, do: :foo
...> end

iex> defmodele ForDocBar do
...>   require Mixin
...>   Mixin.include ForDocFoo
...> end

iex> ForDocBar.foo
:foo

How is it better than import/2?

When import/2.

iex> defmodule A do
...>   def a do
...>     :a
...>   end
...> end

iex> defmodule B do
...>   import A
...> end

iex> B.a()
** (UndefinedFunctionError) function B.a/0 is undefined or private

** When Mixin.include. **

iex> defmodule A do
...>   def a do
...>     :a
...>   end
...> end

iex> defmodule B do
...>   require Mixin
...>   Mixin.include A
...> end

iex> B.a()
:a