Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

120 lines (94 sloc) 2.539 kb

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}
                  ]}
     ]}
   ]},
   ...
 ]}.
Jump to Line
Something went wrong with that request. Please try again.