LoadError: MethodError: "Method too new to be called from this world context." in Julia
LoadError: MethodError: "Method too new to be called from this world context." in Julia
谁能用简单的语言解释为什么会出现这个错误,以及除了不将代码放在函数中 main
之外如何避免错误?
函数string_to_func
请参考问题。
作品:
using SymPy
function string_to_func(function_string)
func_lambdify = lambdify(SymPy.sympify(function_string), invoke_latest=false)
@eval func(x, y, z) = ($func_lambdify)(x, y, z)
return Nothing
end
function_string = "x + y + z"
string_to_func(function_string)
result = func(1, 2, 3)
抛出错误:
using SymPy
function string_to_func(function_string)
expr = lambdify(SymPy.sympify(function_string), invoke_latest=false)
@eval func(x, y, z) = ($expr)(x, y, z)
return Nothing
end
function main()
function_string = "x + y + z"
string_to_func(function_string)
result = func(1, 2, 3)
end
main()
匿名错误消息:
ERROR: LoadError: MethodError: no method matching func(::Int64, ::Int64, ::Int64)
The applicable method may be too new: running in world age 29676, while current world is 29678.
Closest candidates are:
func(::Any, ::Any, ::Any) at path_to_folder\test.jl:5 (method too new to be called from this world context.)
Stacktrace:
[1] main()
@ Main path_to_folder\test.jl:12
[2] top-level scope
@ path_to_folder\test.jl:15
in expression starting at path_to_folder\test.jl:15
您需要使用 Base.invokelatest
调用 func
,即
function main()
function_string = "x + y + z"
string_to_func(function_string)
result = Base.invokelatest(func, 1, 2, 3)
end
有关世界年龄的更多详细信息以及此处需要 invokelatest
的原因,请参阅 the manual。
我还应该提到 GeneratedFunctions.jl,它可以避免与 invokelatest
相关的一些开销,尽管它有自己的警告,因为它有点像 hack。
谁能用简单的语言解释为什么会出现这个错误,以及除了不将代码放在函数中 main
之外如何避免错误?
函数string_to_func
请参考问题
作品:
using SymPy
function string_to_func(function_string)
func_lambdify = lambdify(SymPy.sympify(function_string), invoke_latest=false)
@eval func(x, y, z) = ($func_lambdify)(x, y, z)
return Nothing
end
function_string = "x + y + z"
string_to_func(function_string)
result = func(1, 2, 3)
抛出错误:
using SymPy
function string_to_func(function_string)
expr = lambdify(SymPy.sympify(function_string), invoke_latest=false)
@eval func(x, y, z) = ($expr)(x, y, z)
return Nothing
end
function main()
function_string = "x + y + z"
string_to_func(function_string)
result = func(1, 2, 3)
end
main()
匿名错误消息:
ERROR: LoadError: MethodError: no method matching func(::Int64, ::Int64, ::Int64)
The applicable method may be too new: running in world age 29676, while current world is 29678.
Closest candidates are:
func(::Any, ::Any, ::Any) at path_to_folder\test.jl:5 (method too new to be called from this world context.)
Stacktrace:
[1] main()
@ Main path_to_folder\test.jl:12
[2] top-level scope
@ path_to_folder\test.jl:15
in expression starting at path_to_folder\test.jl:15
您需要使用 Base.invokelatest
调用 func
,即
function main()
function_string = "x + y + z"
string_to_func(function_string)
result = Base.invokelatest(func, 1, 2, 3)
end
有关世界年龄的更多详细信息以及此处需要 invokelatest
的原因,请参阅 the manual。
我还应该提到 GeneratedFunctions.jl,它可以避免与 invokelatest
相关的一些开销,尽管它有自己的警告,因为它有点像 hack。