Skip to content
This code implenets decorators for erlang.
Erlang
Latest commit 221bec2 Mar 14, 2014 @egobrain Tag 0.2.1
Failed to load latest commit information.
examples Init commit Jun 21, 2012
src External decorators Mar 13, 2014
test External decorators Mar 13, 2014
.gitignore Added .gitignore Mar 13, 2014
Makefile External decorators Mar 13, 2014
README.md External decorators Mar 13, 2014
rebar.config Removed debug info Mar 14, 2014

README.md

erlang_decorators

This code implenets decorators for erlang.

Usage

Internal

First of all you need set {parser_transform, decorators} compile option.

-compile([{parse_transform, decorators}]).

and then specify decorate attribute before funcion definition.

-decorate(DecoratorSpec).
my_function(...) -> ... .

DecoratorSpec:

  • {Module :: module(), Fun :: atom()} - will call Module:Fun(F, Args), where F is function with arity 1, which takes a list as argumets and apply this list to original function, and Args is a decorated function arguments list.
  • {Module :: module(), Fun :: atom(), Args :: [any()]} - same as previous, but you can specify aditional decorator function arguments. `Module:Fun(F, Args, Arg1, Arg2, ...) will be called.
  • {Module :: module(), Fun :: atom(), Args :: [any()], verbose} - if you specify verbose option, the third decorator function argument will be an a tuple of original function name and code line: {FuncName :: atom(), Line :: non_neg_integer()}. `Module:Fun(F, Args, {FunName, Line}, Arg1, Arg2, ...) will be called.

External

You can also specify decorators from outside of module at compile time.

First of all you must tell compiler to use decorators parse_transform globaly. And then specify compile decorate option, which describes how to decorate modules functions. Syntax:

{decorate,
    [
     {Module :: module(),
     [
      {{Fun :: atom(), Arity :: non},
       [DecoratorSpec, ...]},
      ...
     ],
     ...
    ]}

Examples

Internal

-module(my_module).

-compile([{parse_transform, decorators}]).

%% decorator
-export([
         log_result/2
        ]).

%% Decorated function
-export([
         foo/1
        ]).

log_result(Fun, Args) ->
    Result = Fun(Args),
    io:format("Result is: ~p", [Result]),
    Result.

-decorate({?MODULE, log_result}).
foo(Bar) ->
    Bar*100.

External

rebar.config

{erl_opts,
 [
  {parse_transform, decorators},
  {decorate,
   [
    {my_module1,
     [
      {{fun1, 0}, [{my_decorators, decorator1}]},
      {{fun2, 0}, [
                   {my_decorators, decorator1},
                   {my_decorators, decorator2}
                  ]},
     ]},
    {my_module2,
     [
      {{fun2, 0}, [
                   {my_decorators, decorator3, [tag]},
                   {my_decorators, decorator4, [tag], verbose}
                  ]}
     ]}
   ]},
   ...
 ]}.
Something went wrong with that request. Please try again.