I’m not sure I understand what you want to achieve here… Could you please explain how the last code you posted relates to the title of this thread? Or maybe take a step back and explain what your bigger problem is?
Also, consider your proposed solution:
macro foo(f, x)
Expr(:call, esc(f), esc(x))
end
function test(f, x)
return @foo(f, x)
end
which benchmarks like this (BenchmarkTools
, which is more reliable than just @time
since it avoids measuring compilation times):
julia> @btime test(length, "justanotherText");
15.426 ns (0 allocations: 0 bytes)
julia> p=test(length, "justanotherText");
julia> println(p, " : ", typeof(p))
15 : Int64
Is this not a rather complicated way of applying a function to a value, like for example:
function apply(f, x)
f(x)
end
julia> @btime apply(length, "justanotherText");
15.062 ns (0 allocations: 0 bytes)
julia> p = apply(length, "justanotherText");
julia> println(p, " : ", typeof(p))
15 : Int64
or even a plain
julia> @btime length("justanotherTest")
15.421 ns (0 allocations: 0 bytes)
15
These ways of expressing things have the same performance as your proposed solution, which is arguably more difficult to understand; wouldn’t you think?