sorbet - Sorbet:检查包含目标模块的类
问题描述
SorbetT.class_of
可用于匹配后代类,但不能用于匹配包含模块的类:
module X; end
class A; end
class B < A; include X; end
B.ancestors # => [B, X, A, Object, PP::ObjectMixin, Kernel, BasicObject]
sig { params(x: T.class_of(A)).void }
def is_a(x); end
is_a(A) # => ok
is_a(B) # => ok
sig { params(x: T.class_of(X)).void }
def is_b(x); end
is_b(X) # => ok
is_b(B) # => error
根据https://sorbet.org/docs/class-of,这是因为 B 不是 X 的 singleton_class 的实例:
B.is_a?(A.singleton_class) # => true
B.is_a?(B.singleton_class) # => true
B.is_a?(X.singleton_class) # => false
但没有提供替代建议。有没有办法根据类的祖先的内容进行类型检查?即类似的东西class_of
可以用来检查模块中的类混合吗?
解决方案
推荐阅读
- html - 背景图像出现在选择框 HTML 上
- html - 使用 CSS 内容属性设置图像并使其不拉伸
- c - How to print the nth term of a string using scanf function?
- c# - Drive API v3,C# - 文件夹层次结构
- reactjs - 如何在 React 中重构 switch 语句的代码?
- amazon-web-services - 如何将 AWS 弹性搜索迁移到 Azure 弹性搜索服务?
- kubernetes - 从 EOF metric-server kubernetes 获取 TLS 握手错误
- regex - 如何从perl中的文本文件中提取动态生成的访问令牌?
- angular - 运行Angular模板时VS中的dist目录在哪里?
- c++ - 挂钩 WH_GETMESSAGE 消息