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

suggestion: define at-invokelatest macro: #37971

Merged
merged 3 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,32 @@ function _kwdef!(blk, params_args, call_args)
blk
end

"""
@invokelatest f(args...; kwargs...)

Provides a convenient way to call [`Base.invokelatest`](@ref).
`@invokelatest f(args...; kwargs...)` will simply be expanded into
`Base.invokelatest(f, args...; kwargs...)`.
"""
macro invokelatest(ex)
is_expr(ex, :call) || throw(ArgumentError("a call expression f(args...; kwargs...) should be given"))

f = first(ex.args)
args = []
kwargs = []
for x in ex.args[2:end]
if is_expr(x, :parameters)
append!(kwargs, x.args)
elseif is_expr(x, :kw)
push!(kwargs, x)
else
push!(args, x)
end
end
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the loop is not necessary --- wouldn't just splatting all of ex.args[2:end] into the expression work?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

na, I think the current parser requires the loop to correctly handle keyword arguments, e.g.:

julia> macro invokelatest(ex)
           @assert is_expr(ex, :call) "call expression f(args...; kwargs...) should be given"
       
           f = first(ex.args)
           args = ex.args[2:end]
           return esc(:($(GlobalRef(Base, :invokelatest))($(f), $(args...))))
       end
@invokelatest (macro with 1 method)

julia> @macroexpand @invokelatest printstyled(stdout, "blue"; color = :blue)
:(Base.invokelatest(printstyled, $(Expr(:parameters, :($(Expr(:kw, :color, :(:blue)))))), stdout, "blue"))

julia> @invokelatest printstyled(stdout, "blue"; color = :blue)
ERROR: syntax: invalid syntax ; color = :blue
Stacktrace:
 [1] top-level scope
   @ none:1


esc(:($(GlobalRef(Base, :invokelatest))($(f), $(args...); $(kwargs...))))
end

# testing

"""
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ Base.hasmethod
Core.applicable
Core.invoke
Base.invokelatest
Base.@invokelatest
new
Base.:(|>)
Base.:(∘)
Expand Down
25 changes: 25 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,31 @@ let foo() = begin
@test foo() == 1
end

module atinvokelatest
f(x) = 1
g(x, y; z=0) = x * y + z
end

let foo() = begin
@eval atinvokelatest.f(x::Int) = 3
return Base.@invokelatest atinvokelatest.f(0)
end
@test foo() == 3
end

let foo() = begin
@eval atinvokelatest.f(x::Int) = 3
return Base.@invokelatest atinvokelatest.f(0)
end
@test foo() == 3

bar() = begin
@eval atinvokelatest.g(x::Int, y::Int; z=3) = z
return Base.@invokelatest atinvokelatest.g(2, 3; z=1)
end
@test bar() == 1
end

# Endian tests
# For now, we only support little endian.
# Add an `Sys.ARCH` test for big endian when/if we add support for that.
Expand Down