简体   繁体   English

带有类参数的函数的冰糕签名?

[英]Sorbet signature for a function with class argument?

How do I write a signature for the following archive_all function? 如何为以下archive_all函数编写签名?

sorbet.run 果汁冰糕

# typed: true
extend T::Sig

module Archivable
  def archive
  end
end
class Book
  extend Archivable
end
class Magazine
  extend Archivable
end

sig {params(klass: T.class_of(Archivable)).void}
def archive_all(klass)
  klass.archive
end

archive_all(Book) 
archive_all(Magazine)

Sorbet error: 冰糕错误:

editor.rb:17: Method archive does not exist on T.class_of(Archivable) https://srb.help/7003
    17 |  klass.archive
          ^^^^^^^^^^^^^
    editor.rb:5: Did you mean: Archivable#archive?
     5 |  def archive
          ^^^^^^^^^^^

You want to be able to pass any instance of Archivable . 您希望能够传递任何Archivable实例。 The correct way to do this is to use a feature of Sorbet called a class type : 正确的方法是使用Sorbet的一种称为类类型的功能

sig {params(klass: Archivable).void}

Observe . 观察一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Sorbet在Ruby中定义super class中定义的passthrough方法的方法签名 - Define method signature for passthrough method defined in super class in Ruby with Sorbet 如何为Sorbet中的属性定义哈希的签名? - How to define a signature for a hash with attributes in Sorbet? 冰糕没有发现 Hash 签名的明显不匹配 - Sorbet not picking up obvious mismatch of Hash signature 如何使用 Sorbet 为块编写类型签名? - How to write a type signature for a block with Sorbet? 在冰糕中,你能指定一个类型是 class 的后代吗? - In sorbet, can you specify that a type is a descendent of a class? Sorbet 错误 - UserEmbeds 的超级 class UserBase 并非源自 Class - Sorbet Error - The Super class UserBase of UserEmbeds does not derive from Class Sorbet - 从通用 class 继承时的具体类型 - Sorbet - concrete type when inheriting from generic class Sorbet 没有报告丢失的方法实现(与 RBI 签名相比),我在这里做错了什么吗? - Sorbet doesn't report a missing method implementation (compared to an RBI signature), am I doing something wrong here? 为什么 Sorbet 认为我在 RBI 文件中提供显式签名的方法不存在? - Why does Sorbet think that a method I provided an explicit signature for in an RBI file doesn't exist? 如果 Sorbet 已静态检查代码,是否会在运行时触发无效签名错误? - Can invalid signature errors ever trigger at runtime if code has been statically checked by Sorbet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM