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

时间:2019-08-02 19:09:11

标签: ruby sorbet

如何为以下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)

冰糕错误:

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
          ^^^^^^^^^^^

1 个答案:

答案 0 :(得分:2)

您希望能够传递Archivable的任何实例。正确的方法是使用Sorbet的class type功能:

sig {params(klass: Archivable).void}

Observe