Is there a way to make macros consume forms past a newline?

proposal
macros

#1

Reading through the recent thread asking if there’s a way to mimic the behavior of Python’s decorators, it occurred to me that, while semantically Julia’s macros are strictly more powerful than method decorators, stylistically there’s not a good way (that I can think of) to give Julia code the same shape.

That is, this works in Julia:

@mydecorator function myfunction(args)
    dostuff(args)
end

but this doesn’t:

@mydecorator
function myfunction(args)
    dostuff(args)
end

…unless I’m missing something?

So, is there a good way to make the second form work? If not, is there some reason it couldn’t be made to work? (I realize that, with v1.0 just around the corner, it’s probably far too late to be making these sorts of suggestions for 1.0…but maybe in 2.0?)


#2

AFAIK with the newline, it is considered a complete expression, so it is not possible to make it wait for the function ....

I think it is best to let go of expectations on how expressions should look like based on other languages. Coming from Common Lisp, I missed the nice delimiting aspect of S-expressions with macros because I could not write

@my_block_macro
    ...
end

without a begin, then saw how packages make use of existing forms like for for blocks (eg ProgressMeter.jl is a nice example). IMO it is best to work with the language as it is.


#3

Probably wise advice. The reason I bring this up is that, aside from the thread I linked, I have found myself wanting this on my own once or twice, independent of any desire for congruity with other languages. What gets me is that there’s no way around the line-ending restriction. For example:

macro foo(x)
    println("Foo got: $x")
end

@foo \
"Hello"
# => "Foo got: \"

Even C macros let you escape newlines!

Ok, ok…it’s not critical. If I’m the only one, I’m happy to let it go. For one thing, I can’t really think of a good way to selectively allow macro calls to bypass a newline without breaking a lot of the rest of Julia (though I’ve not spent too long thinking about it). This question was more about trying to find any clever hacks around newline, and I think ProgressMeter.jl’s for hack definitely counts.


#4

You can use begin end blocks.