朱莉娅:基函数的意外扩展



我有以下不完整的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:方法定义错误:函数Base.step必须为显式导入以进行扩展LoadError:方法定义中出错:函数Base.reset必须为显式导入以扩展

我不知道Base.step和Base.reset是什么,我不想扩展它们。

有没有办法在不扩展基本函数的情况下保留这些函数名?如果我只是用完全不相关的方法扩展基本函数,会有问题吗?

我真的不想更改函数的名称,因为我想让它们与OpenAI Gym 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

然后您可以在模块内直接将它们称为stepreset。在模块之外,您必须对它们进行如下限定:Gym.stepGym.reset

此外,请注意,只有在Main模块中引入stepreset之后,才能尝试扩展它们(例如,通过调用或引用它们(。因此,当开始一个干净的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

最新更新