如何使用 Sorbet 为块编写类型签名?
How to write a type signature for a block with Sorbet?
我目前正在尝试向一些核心 Rails 方法添加类型,其中之一是 respond_to
。它可以与块一起使用,如下所示:
respond_to do |format|
format.html
format.json { render json: @companies }
end
我遇到的问题是如何准确输入,因为 the docs on T.proc
are pretty minimal. The format
parameter is an instance of ActionController::MimeResponds::Collector
。块不需要 return 任何东西(例如,它不像 Array#select
那样计算块并且块 return 是一个布尔值)。
我想这就是你想要写签名的方式?:
sig do
params(
mimes: T.nilable(Symbol),
block: T.proc.params(arg0: ActionController::MimeResponds::Collector).void
).void
end
def respond_to(*mimes, &block); end
(我们可以暂时忽略*mimes
参数,这不重要)
这似乎可行,但我只是想确保我理解 T.proc
应该使用的方式。
(注意有 an issue with blocks that are nilable causing a regression to T.untyped
,但这不是我目前感到困惑的地方)
对我来说似乎是签名的正确用法。
顺便说一句,非常欢迎添加文档!
我目前正在尝试向一些核心 Rails 方法添加类型,其中之一是 respond_to
。它可以与块一起使用,如下所示:
respond_to do |format|
format.html
format.json { render json: @companies }
end
我遇到的问题是如何准确输入,因为 the docs on T.proc
are pretty minimal. The format
parameter is an instance of ActionController::MimeResponds::Collector
。块不需要 return 任何东西(例如,它不像 Array#select
那样计算块并且块 return 是一个布尔值)。
我想这就是你想要写签名的方式?:
sig do
params(
mimes: T.nilable(Symbol),
block: T.proc.params(arg0: ActionController::MimeResponds::Collector).void
).void
end
def respond_to(*mimes, &block); end
(我们可以暂时忽略*mimes
参数,这不重要)
这似乎可行,但我只是想确保我理解 T.proc
应该使用的方式。
(注意有 an issue with blocks that are nilable causing a regression to T.untyped
,但这不是我目前感到困惑的地方)
对我来说似乎是签名的正确用法。
顺便说一句,非常欢迎添加文档!