Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

An example implementation of Python style decorators in Erlang

Notifications You must be signed in to change notification settings

TheRealReal/erlang_decorators

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Erlang Decorators

This repository has been archived.

📚 Archived Status: This repository is no longer actively maintained or updated. It is being preserved for historical reference or potential future use. Issues, pull requests, and contributions are no longer accepted.

An early implementation of python style decorators for Erlang.

Quick start

Add the following to your rebar.config:

{deps,
 [
  {decorators, "", {git, "git://github.com/chrisavl/erlang_decorators.git", {branch, "master"}}}
 ]
}.

Add -compile([{parse_transform, decorators}]). to any source file using decorators, or add {erl_opts, [{parse_transform, decorators}]}. to your rebar.config.

Usage

There are 4 ways to decorate a function:

-decorate(my_decorator). %% local function, no decorator data
-decorate({my_decorator, [{option, value}]}). %% local function, with data
-decorate({my_module, my_decorator}). %% external function, no decorator data
-decorate({my_module, my_decorator, [{option, value}]}). %% external function, with data

If no data/options is specified it defaults to the empty list.

Example:

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

-export([demo/0]).

my_decorator(OriginalFun, OriginalArgs, _UnusedData = []) ->
    Start = erlang:now(),
    Result = apply(OriginalFun, OriginalArgs),
    io:format("call to ~p (args: ~p) took: ~f ms~n",
              [OriginalFun, OriginalArgs,
               timer:now_diff(now(), Start) / 1000]),
    Result.

%%-decorate({mymod, my_decorator}). % If decorator is an external call
-decorate(my_decorator). %% Since my_decorator is local, use shorthand notation
my_long_running_function(A, B) ->
    timer:sleep(100),
    A + B.

demo() ->
    3 = my_long_running_function(1, 2).

About

An example implementation of Python style decorators in Erlang

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Erlang 100.0%