Actually, I have just figured out to do this macro without Base.invokelatest
and updated SyntaxTree
:
julia> using SyntaxTree, BenchmarkTools
julia> g = genfun(:(-(cos(x))),[:x])
(::#3) (generic function with 1 method)
julia> @btime $g(1.0)
40.790 ns (0 allocations: 0 bytes)
-0.5403023058681398
julia> f = @genfun -cos(x) [x]
(::#3) (generic function with 1 method)
julia> @btime $f(1.0)
40.801 ns (0 allocations: 0 bytes)
-0.5403023058681398
Now the evaluation is much faster because it is not using Base.invokelatest
anymore. The implementation
macro genfun(expr,args)
:($(Expr(:tuple,args.args...))->$expr)
end
genfun(expr,args) = :(@genfun $expr [$(args...)]) |> eval
is very simple to write in Julia in the end, it is now part of the SyntaxTree package.