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

This branch is 3 commits ahead of chrisavl/erlang_decorators:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

25a8ca0 · Oct 12, 2023

History

20 Commits
Apr 29, 2016
Nov 1, 2013
Apr 28, 2016
Oct 12, 2023
Apr 28, 2016
Apr 28, 2016

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%