为什么 Sorbet 需要 sig 和 T.let 才能进行类型检查?
Why are both sig and T.let required for Sorbet to type check?
我将我的文件设置为 typed: strict
,并将我的 initialize
方法设置为获取浮点数数组,但是 srb tc
报告我必须使用 T.let
方法主体中的断言:
# typed: strict
class Point
extend T::Sig
sig { params(c: T::Array[Float]).returns(t::Array[Float]) }
def initialize(c)
@c = c
end
end
Sorbet 不能从签名中推断出 @c
的类型吗?
编辑:自 2019-12 年起,情况不再如此(参见 PR #2230)。现在这段代码完全有效(注意构造函数的签名将 void
声明为 return 类型):
# typed: strict
class Point
extend T::Sig
sig { params(c: T::Array[Float]).void }
def initialize(c)
@c = c # Sorbet knows that c is a `T::Array[Float]`, so it assigns that type to @c
end
end
之前:
这是 Sorbet 中的一个已知限制:“Why do I need to repeat types from the constructor?”
TL;DR:
[Sorbet] cannot reuse static type knowledge in order to automatically determine the type of an instance or class variable.
在#1666 Seemingly unnecessary type annotation of instance variables
中也有报道
我将我的文件设置为 typed: strict
,并将我的 initialize
方法设置为获取浮点数数组,但是 srb tc
报告我必须使用 T.let
方法主体中的断言:
# typed: strict
class Point
extend T::Sig
sig { params(c: T::Array[Float]).returns(t::Array[Float]) }
def initialize(c)
@c = c
end
end
Sorbet 不能从签名中推断出 @c
的类型吗?
编辑:自 2019-12 年起,情况不再如此(参见 PR #2230)。现在这段代码完全有效(注意构造函数的签名将 void
声明为 return 类型):
# typed: strict
class Point
extend T::Sig
sig { params(c: T::Array[Float]).void }
def initialize(c)
@c = c # Sorbet knows that c is a `T::Array[Float]`, so it assigns that type to @c
end
end
之前:
这是 Sorbet 中的一个已知限制:“Why do I need to repeat types from the constructor?”
TL;DR:
[Sorbet] cannot reuse static type knowledge in order to automatically determine the type of an instance or class variable.
在#1666 Seemingly unnecessary type annotation of instance variables
中也有报道