invoke
会做的。 (请注意,尽管这可能不会编译为漂亮的专用代码)
这里的问题是朱莉娅专注于类型。
也就是说,它为传递给它的每个类型组合编译该函数的自定义版本。
由于函数在 julia 0.5 中有一个类型
(每个函数都是单例类型。)
这导致它专注于该功能
在 0.5-rc0 上测试
julia> userfunc(str, n) = str ^ (n*n)
WARNING: Method definition userfunc(Any, Any) in module Main at REPL[16]:1 overwritten at REPL[20]:1.
userfunc (generic function with 1 method)
julia> function programfunc(func, a, b)
invoke(func, (typeof(a), typeof(b)), a, b)
end
programfunc (generic function with 1 method)
julia> programfunc(userfunc, "hello", 3)
"hellohellohellohellohellohellohellohellohello"
julia> userfunc(str, n) = str ^ (n)
WARNING: Method definition userfunc(Any, Any) in module Main at REPL[16]:1 overwritten at REPL[20]:1.
userfunc (generic function with 1 method)
julia> programfunc(userfunc, "hello", 3)
"hellohellohello"
请注意,这也适用于#265 https://github.com/JuliaLang/julia/issues/265
julia> foo(x)=2*x
foo (generic function with 1 method)
julia> function g(x)
invoke(foo, (typeof(x),), x)
end
g (generic function with 1 method)
julia> g(2)
4
julia> foo(x)=3*x
WARNING: Method definition foo(Any) in module Main at REPL[1]:1 overwritten at REPL[10]:1.
foo (generic function with 1 method)
julia> g(2)
6