【问题标题】:Why are both sig and T.let required for Sorbet to type check?为什么 Sorbet 需要 sig 和 T.let 才能进行类型检查?
【发布时间】:2020-01-02 12:53:51
【问题描述】:

我将我的文件设置为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 的类型吗?

【问题讨论】:

    标签: ruby sorbet


    【解决方案1】:

    编辑:从 2019-12 开始,情况不再如此(请参阅 PR #2230)。现在这段代码完全有效(请注意,构造函数的签名将void 声明为返回类型):

    # 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] 不能重用静态类型知识来自动确定实例或类变量的类型。

    #1666 Seemingly unnecessary type annotation of instance variables也有报道

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 2017-04-05
      • 2013-04-10
      • 2020-01-01
      • 2015-04-14
      • 1970-01-01
      相关资源
      最近更新 更多