New syntax for multi-line macrocalls

proposal

#1

Hi! :smile: This is a proposal for an additional macrocall syntax which will parse past newlines until an end is encountered. The idea is to have a syntax which allows using a macro more akin to reserved keyword blocks such as function, struct, etc., without requiring manual use of begin ... end. The syntax proposed here uses @@ because this is currently a syntax error, but better alternatives may exist.

As an example, suppose I want to save three characters when defining functions, so I define the macro:

macro fun(signature, lines...)
    quote
        function $(signature.args[1])($(signature.args[2:end]...))
            $(lines...)
        end
    end |> esc
end

Then with the new syntax rule, the following would be a valid macrocall:

@@fun f(x)
    y = x^2
    return y
end

and equivalent to

@fun f(x) y = x^2 return y

A more useful example might be something like:

macro loop(before, condition, body, after)
    quote
        $before
        while $condition
            $body
        end
        $after
    end |> esc
end

resulting in the following macrocall:

@@loop
    begin
        x = 1
        y = 10
    end
    x < y
    x += 1
    println(x)
end

being equivalent to:

@loop begin
    x = 1
    y = 10
end begin
    x < y
end begin
    x += 1
end begin
    println(x)
end

Of course, these are simplified examples to illustrate the main idea, and actual macros which make use of this syntax would presumably add a lot of boilerplate around the transformed expressions.


#2

IIRC @jeff.bezanson and I had discussed this very syntax idea at some point.


split this topic #3

3 posts were split to a new topic: Pattern matching for macros


#4

Actually, I personally did not think of this suggestion as being related to pattern matching, but instead being all about the aesthetics; having a clean way to split arguments to a macro over several lines. That it shares some features with pattern matching is to me tangential. Having a “line continuation” character could achieve the same purpose, but I personally don’t know which character should be used for this and I am not super fond of how it reads (e.g., \ in Python).


#5

Line continuation PR: