ruby - Sorbet cannot resolve constant even though it is defined
问题描述
Given:
# typed: true
module X
class Y
end
end
module X
class X
def y
X::Y
end
end
end
Sorbet gives error :
editor.rb:6: Unable to resolve constant Y https://srb.help/5002
6 | X::Y
Why sorbet given error even though X::Y is defined?
解决方案
因为这就是 ruby 中常量查找的工作方式。粗略地说,它尝试从最里面的嵌套开始解析名称。因此,在你的X::Y
它解析X
为class X
没有Y
.
改为使用::X::Y
强制从顶层查找。
推荐阅读
- c++ - 在这种情况下,三元运算符与 C 风格的指针向上转换相结合会调用未定义的行为吗?
- python - 单元:无监督图像到图像翻译网络
- php - Drupal 中的 PHP 7.3 已弃用命名空间断言
- javascript - 我有一个类似于下面输出的 mongodb 数据响应,并且想要对数组字段的结果进行分组
- python - Discord.py 嵌入文本文件
- javascript - 在刀片模板中包含 js 文件
- python - Heroku cli - 如何通过命令行运行 python 函数?
- css - 定位视频并使其像图像一样在背景中重复
- asp.net - jqGrid如何动态多选?
- javascript - 在 laravel 中使用 vue.js 按钮单击时无法显示模式