Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow @foo{...} macro calls #34498

Closed
stevengj opened this issue Jan 24, 2020 · 3 comments · Fixed by #34505
Closed

allow @foo{...} macro calls #34498

stevengj opened this issue Jan 24, 2020 · 3 comments · Fixed by #34505
Labels
parser Language parsing and surface syntax speculative Whether the change will be implemented is speculative

Comments

@stevengj
Copy link
Member

stevengj commented Jan 24, 2020

I noticed that you cannot invoke a macro as @foo{...} — you have to do @foo {...}. Can we remove the requirement of a space before the brace? This would be useful for macros to construct parameterized types, for example.

Background: In a recent discourse discussion, I was looking at implementing a macro @NamedTuple that allows you to write @NamedTuple{raw::Vector{Float64}, value::Int} as a synonym for NamedTuple{(:raw, :value),Tuple{Array{Float64,1},Int64}}, via the implementation:

macro NamedTuple(ex)
    Meta.isexpr(ex, :braces) || error("@NamedTuple expects {...}")
    all(e -> Meta.isexpr(e, :(::)), ex.args) || error("@NamedTuple a sequence of name::type expressions")
    vars = [QuoteNode(e.args[1]) for e in ex.args]
    types = [e.args[2] for e in ex.args]
    return :(NamedTuple{($(vars...),), Tuple{$(types...)}})
end

This works with @NamedTuple {raw::Vector{Float64}, value::Int}, but omitting the space gives

julia> @NamedTuple{raw::Vector{Float64}, value::Int}
ERROR: syntax: invalid macro usage "@(NamedTuple{raw::Vector{Float64}, value::Int})"

Since this is currently a syntax error (that is, parsing fails), supporting it would be non-breaking.

@stevengj stevengj added speculative Whether the change will be implemented is speculative parser Language parsing and surface syntax labels Jan 24, 2020
@JeffBezanson
Copy link
Member

👍 We did this for @m[...], so this totally makes sense.

@stevengj
Copy link
Member Author

Looks like this patch should be sufficient:

diff --git a/src/julia-parser.scm b/src/julia-parser.scm
index 5172748c55..7578c1f741 100644
--- a/src/julia-parser.scm
+++ b/src/julia-parser.scm
@@ -2394,6 +2394,10 @@
          `(macrocall ,(macroify-name (cadr call))
                      ,startloc
                      ,@(cddr call)))
+        ((and (pair? call) (eq? (car call) 'curly))
+         `(macrocall ,(macroify-name (cadr call))
+                     ,startloc
+                     (braces ,@(cddr call))))
         ((and (pair? call) (eq? (car call) 'do))
          `(do ,(macroify-call s (cadr call) startloc) ,(caddr call)))
         (else

@c42f
Copy link
Member

c42f commented Jul 15, 2020

Haha, I knew this didn't work in <=1.4 so I thought I'd just PR a fix for it directly... but for some reason couldn't find the right place in the source.

Turns out it was fixed already 😆 Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax speculative Whether the change will be implemented is speculative
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants