Deco - Minimalist Function Decorators for Elixir.

hexdocs.

Yes, yet another package for decorating elixir functions :).

However, deco's core is minimalist in comparission to others, uses much less magic, does not overrides anything like Kernel.def nor any operator.

deco has only one macro, decorators themselves are just plain functions Macro.t() -> Macro.t(), and imposes no run-time overhead because function decoration is performed at compile-time.

Usage

The syntax is deco DECORATORS in FORM where decorators is a tuple of one or more decorators, and form is tipically a function definition.

decorators are just plain functions you write that take the AST of FORM and just return a modified AST of it. The Deco module has some convenience functions for updating the AST.

Since deco runs at compile time, you cannot use functions being defined in the same module that is using deco. Normally it's better to define your decorators on a separate module.

The following example taken from a test decorates a function with a Logger tracer that will print the arguments given before applying to the original function, and the its final result.

deco { Example.trace() } in
def foo(x) do
x
end

Decorators can take arguments, the AST will be prepended to the list of arguments given by you. For example, Deco.pipe_result will as it name implies just pipe the function return value into the code given as argument to the decorator.

deco { Deco.pipe_result(to_string) } in
def foo(x) when is_atom(x) do
x
end

Decorators can be composed, the one at the end will take the original AST and produce a new one for the one on top of it.

deco {
Deco.pipe_result(String.capitalize()),
Deco.pipe_result(String.reverse()),
Deco.pipe_result(to_string)
} in
def foo(x) do
x
end
foo(:john)
=> "Nhoj

For more examples, see the tests and the use the source, Luke

Installation

def deps do
[
{:deco, "~> 0.1"}
]
end