4

In some languages (like Python) there are function decorators which appear like macros and sit above a function definition. Decorators provide some additional functionality to the function itself.

Does Julia support this idea of function decorators in any way? Would it be possible to use macros to accomplish the same goal?

2
  • 3
    In Python, decorators are nothing more than functions that take a function as an argument and return a modified one in some way. In Julia, we have the added complexity that the "function" we're defining is not a whole function but potentially a small piece of a multimethod, which adds a bit of complexity. I'm curious whether something like this is easily doable. Commented Apr 25, 2021 at 4:40
  • 2
    I haven't written many macros but I think macros are typically how one "decorates" methods in Julia. It doesn't really work the same way because macros play into the compilation process (Julia is compiled vs. Python is interpreted). You can make something that works more like Python's decorators by writing a method that takes in a method and returns a method. Just be aware of the problems with defining internal methods conditionally.
    – BatWannaBe
    Commented Apr 25, 2021 at 7:57

2 Answers 2

9

Here is one way to achieve this with a macro using ExprTools.jl:

import ExprTools

macro dec(decorator, inner_def)
    inner_def = ExprTools.splitdef(inner_def)
    outer_def = copy(inner_def)
    fname = get(inner_def, :name, nothing)
    if fname !== nothing
        @assert fname isa Symbol
        inner_def[:name] = Symbol(fname, :_inner)
    end
    outer_def[:body] = Expr(:call,
        :($decorator($(ExprTools.combinedef(inner_def)))),
        get(outer_def, :args, [])...,
        get(outer_def, :kwargs, [])...,
    )
    return esc(ExprTools.combinedef(outer_def))
end
julia> _time(f) = (x...) -> (t0=time(); val=f(x...); println("took ", time()-t0, " s"); val)
_time (generic function with 1 method)

julia> @dec _time function foo(x, y)
           return x + y
       end
foo (generic function with 1 method)

julia> foo(1, 2)
took 9.5367431640625e-7 s
3

However, at least in my experience, this comes up surprisingly rarely in idiomatic Julia programs. I would first think about whether your specific problem might be better solved just by multiple dispatch with plain higher-order functions and traits, which will usually lend itself to better composability.

2

To expand the comment of @SilvioMayolo, Julia has pretty elegant macro capability, that is more general than the Python decorator. Indeed Julia's macro take as input any expression, and the macro can hook over the Abstract Syntax Tree to return a modified expression.

The keyword you want to look for is "metaprogramming".

Here you can find the official documentation and here a convenient tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.