考虑下面的例子,其中一个功能bar用途@eval重新定义foo调用前
julia> foo() = 1
foo (generic function with 2 methods)
julia> function bar()
@eval foo() = 2 # remember @eval runs at global scope
foo()
end
bar (generic function with 1 method)
julia> bar()
1 # Got old version
julia> function bar2()
@eval foo() = 3 # remember @eval runs at global scope
Base.invokelatest(foo,)
end
bar2 (generic function with 1 method)
julia> bar2()
3
Run Code Online (Sandbox Code Playgroud)
因为到那时,bar调用foo
bar已经被编译,因此foo已经优化为静态调用。(即使在这种情况下也可能内联)。
因此bar看不到foo通过创建的新覆盖@eval
它比较慢,因为它阻止了调用编译为静态调度。
通常,您永远都不需要此代码。尝试避免使用@eval内部函数。很难推理。
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |