Is it possible to define a macro in one line (one-liner macros)?

We have one-liner functions, can we define macros in one line? Like

@my(x) = :(println($(esc(x))))

which is equivalent to

macro my(x)
    :(println($(esc(x))))
end

I guess not, because sometimes = is part of the macro arguments?

You can in the trivial sense of using semi-colons:

julia> macro my(x);  :(println($(esc(x)))); end
@my (macro with 1 method)

julia> y = [10, 1000];

julia> @my y
[10, 1000]

I don’ t think there’s special syntax like the assignment syntax for functions though.

I get that. It just does not look like Julia’s one-liner functions.

True, and tbh I wouldn’t recommend that syntax in practice - a couple of newlines are worth it for the sake of readability. Especially since macros are on average more complex/harder to understand than functions.

A macro can return a symbol that is the target for an assignment. For example

macro my(x)
    Symbol(string("my_", x))
end

@my(x) = 3 # expands to my_x = 3
3 Likes

Interesting, thanks. However, what I want to simplify is the definition of the macro @my into one line.

My point is that this would break existing code, since the syntax is already used for something else.

5 Likes

Not sure it is a good idea: macros should not be written lightly, and as highlighted above the syntax you want collides with existing, valid constructs.

But, assuming for the sake of the argument that you still want this, it should be possible to write a one-line macro (or rather a 3-line macro) that allows you to write one-line macros :smiley:

julia> macro onelinemacro(expr)
           # we could/should assert that `expr.head == :(=)`, but then this
           # would not be a one-line macro any more :-)
           esc(Expr(:macro, expr.args...))
       end
@onelinemacro (macro with 1 method)

julia> @onelinemacro my_show(x) = :(println($(QuoteNode(x)), " = ", $(esc(x))))
@my_show (macro with 1 method)

julia> let z = 42
           @my_show z
       end
z = 42
4 Likes
var"@my"(mod, source, x) = :(println($(esc(x))))
4 Likes