[英]Sorbet signature for a function with class argument?
How do I write a signature for the following archive_all
function? 如何为以下archive_all
函数编写签名?
# 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}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.