我目前正在尝试向一些核心Rails方法添加类型,其中之一就是respond_to。它可以与块一起使用,如下所示:
respond_to do |format|
format.html
format.json { render json: @companies }
end我遇到的问题是如何准确地输入它,因为the docs on T.proc是相当小的。format参数是ActionController::MimeResponds::Collector的一个实例。块不需要返回任何东西(例如,它不像Array#select那样计算块的值,而块返回一个布尔值)。
我想这就是你想要写签名的方式吧?
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,但这不是我目前感到困惑的地方)
发布于 2019-08-13 00:55:24
在我看来签名的用法是正确的。
顺便说一句,非常欢迎添加到文档中!
https://stackoverflow.com/questions/57453812
复制相似问题