Julia 朱莉娅:基函数的意外扩展
我有以下不完整的代码:Julia 朱莉娅:基函数的意外扩展,julia,Julia,我有以下不完整的代码: mutable struct Env end function step(env, action:UInt32) return ones(8), 1.0, true, Dict() end function reset(env) return ones(8) end 当我尝试使用它时,会出现以下错误: LoadError:方法定义中的错误:function Base.step必须为 显式导入以进行扩展 LoadError:方法定义中的错误:funct
mutable struct Env
end
function step(env, action:UInt32)
return ones(8), 1.0, true, Dict()
end
function reset(env)
return ones(8)
end
当我尝试使用它时,会出现以下错误:
LoadError:方法定义中的错误:function Base.step必须为
显式导入以进行扩展
LoadError:方法定义中的错误:function Base.reset必须为
显式导入以进行扩展
我不知道Base.step和Base.reset是什么,我不想扩展它们
我有没有办法保留这些函数名而不扩展基函数?如果我只是用完全不相关的方法扩展基函数,会有问题吗
我真的不想更改函数的名称,因为我想让它们与API保持一致。在这样的模块中定义它们
module Gym
mutable struct Env
end
function step(env, action::UInt32)
return ones(8), 1.0, true, Dict()
end
function reset(env)
return ones(8)
end
end
然后您可以在模块内直接调用它们,称为步骤
和重置
。在模块之外,您必须像这样对他们进行限定:Gym.step
和Gym.reset
此外-请注意,只有在尝试扩展它们之前(例如,通过调用或引用它们),在Main
模块中引入step
和reset
,您才会遇到此问题。因此,在启动clean Julia会话时,这将起作用:
$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.2 (2018-11-08)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> step(x) = x
step (generic function with 1 method)
但这将失败:
$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.0.2 (2018-11-08)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> step
step (generic function with 4 methods)
julia> step(x) = x
ERROR: error in method definition: function Base.step must be explicitly imported to be extended