Skip to content

spilgames/erl-decorator-pt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Purpose

This application implements a set of transformations in the erlang syntax tree allowing the following:

  • The decorator directive on top of functions in order to add extra functionality to them

  • The ?FUNCTION and ?ARITY macros, returning an atom with the function name corresponding to the current scope and the amount of arguments that function was invoked with. These macros can be used in your decorators.

Usage

There are many ways to include these macros in your projects. Just remember macros and macro debugging in particular are not the simplest tools in the world. A great power comes with a great responsibility.

The most common way of using this applocation is:

  1. Make it available to your application via rebar

  2. Create an .hrl file loading hooking the decorator_pt_core in the compilation process and defining a macro with a representative name for your decorator. i.e. my_decorator.hrl

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

-define(MY_DECORATOR(Options), -decorate({MyCbModule, MyCbFun, {?MODULE, ?FUNCTION, Options}})).

  1. Define the callback function in the callback module to your liking. Keep in mind what the signature of your callback should be:
my_cb_fun(OriginalFun::function(), Args::[tern()],
          {OriginalModule::atom(), OriginalFunctionName::atom(), Opts::[term()]) ->
    FunReturningResult::fun(() -> term()).
  1. When using your decorator, include my_decorator.hrl and decorator_pt.hrl in that order.
-include_lib("my_app/include/my_decorator.hrl").
-include_lib("decorator_pt/include/decorator_pt.hrl").

[...]

?MY_DECORATOR([{opt1, val}]).
my_decorated_fun() ->
    ok.

About

Python like decorators for Erlang (based on Niki Bowe's)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages