Sorbet 高级T.type_参数在冰糕中的应用

Sorbet 高级T.type_参数在冰糕中的应用,sorbet,Sorbet,为了编写泛型方法,我只找到了。 目前未提及类型\u参数。所以我问了一些关于T.type\u参数的高级用法的问题 限制父类型 如何指定只允许特定类型的子类型?(与泛型类相同,使用类型\成员) 例如,只允许使用“Enumerable”类型,因此我可以调用该对象上“Enumerable”中的所有内容 工厂方法 我有一个实例化给定类的对象的方法。(例如,因为它使用的参数应保密)。我怎样才能为它签名 #键入:true 类动物 def初始化(性质秘密);结束 结束 响尾蛇类动物身上不存在方法摇铃 我想我找

为了编写泛型方法,我只找到了。 目前未提及
类型\u参数
。所以我问了一些关于
T.type\u参数的高级用法的问题

  • 限制父类型
  • 如何指定只允许特定类型的子类型?(与泛型类相同,使用
    类型\成员
    ) 例如,只允许使用“Enumerable”类型,因此我可以调用该对象上“Enumerable”中的所有内容

  • 工厂方法
  • 我有一个实例化给定类的对象的方法。(例如,因为它使用的参数应保密)。我怎样才能为它签名

    #键入:true
    类动物
    def初始化(性质秘密);结束
    结束
    响尾蛇类<动物
    def嘎嘎声;结束
    结束
    阶级性质
    扩展T::Sig
    sig{params(animal_cls:T.class_of(animal)).returns(animal)}
    def自我工厂(动物实验室)
    动物分类新(@secret\u dna)
    结束
    结束
    大自然:工厂(响尾蛇)。嘎嘎作响
    #=>动物身上不存在方法摇铃
    
    我想我找到了“1.限制父类型”的答案

    然而,我仍然缺少“2.工厂方法”的解决方案。
    特别是如何通过给定类的泛型类型指定返回值


    答:一,。限制父类型

    它就在五天前的提交日志中

    看起来
    较低
    也可以省略

    #键入:true
    类动物;结束
    猫类<动物类;结束
    类Serval#^^^^^^^^^^错误:上限`

    我想我找到了“1.限制父类型”的答案

    然而,我仍然缺少“2.工厂方法”的解决方案。
    特别是如何通过给定类的泛型类型指定返回值


    答:一,。限制父类型

    它就在五天前的提交日志中

    看起来
    较低
    也可以省略

    #键入:true
    类动物;结束
    猫类<动物类;结束
    类Serval#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^。。。我想你已经把你的答案贴到了另一个问题上了。。。就抄在这里。
    
    #typed: true
    
    class Animal
      def initialize(secret_of_nature); end
    end
    
    class Sidewinder < Animal
      def rattle; end
    end
    
    
    class Nature
      extend T::Sig
    
      sig {params(animal_cls: T.class_of(Animal)).returns(Animal)}
      def self.factory(animal_cls)
        animal_cls.new(@secret_dna)
      end
    end
    
    
    Nature::factory(Sidewinder).rattle
    # => Method rattle does not exist on Animal
    
    # typed: true
    
    class Animal; end
    class Cat < Animal; end
    class Serval < Cat; end
    
    class A
      extend T::Generic
      T1 = type_member(lower: Serval, upper: Animal)
    end
    
    # should pass: Cat is within the bounds of T1
    class B1 < A
      extend T::Generic
      T1 = type_member(fixed: Cat)
    end
    
    # should fail: String is not within the bounds
    class B2 < A
      extend T::Generic
      T1 = type_member(fixed: String)
         # ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: parent lower bound `Serval` is not a subtype of lower bound `String`
         # ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: upper bound `String` is not a subtype of parent upper bound `Animal`
    end
    
    # should pass: the bounds are a refinement of the ones on A
    class C1 < A
      extend T::Generic
      T1 = type_member(lower: Serval, upper: Cat)
    end
    
    # should fail: the bounds are wider than on A
    class C2 < A
      extend T::Generic
      T1 = type_member(lower: Serval, upper: Object)
         # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: upper bound `Object` is not a subtype of parent upper bound `Animal`
    end
    
    # should fail: the implicit bounds of top and bottom are too wide for T1
    class D1 < A
      T1 = type_member
         # ^^^^^^^^^^^ error: parent lower bound `Serval` is not a subtype of lower bound `T.noreturn`
         # ^^^^^^^^^^^ error: upper bound `<any>` is not a subtype of parent upper bound `Animal`
    end