鉴于:
# typed: true
module X
class Y
end
end
module X
class X
def y
X::Y
end
end
end
Run Code Online (Sandbox Code Playgroud)
冰糕给出错误:
editor.rb:6: Unable to resolve constant Y https://srb.help/5002
6 | X::Y
Run Code Online (Sandbox Code Playgroud)
即使定义了 X::Y,为什么 sorbet 也会给出错误?
因为这就是 Ruby 中持续查找的工作原理。粗略地说,它尝试从最内层嵌套开始解析名称。因此,在您的 a 中,X::Y它解析X为class X,而没有Y。
相反,使用::X::Y来强制从顶层进行查找。