ruby - Using Sorbet Generics with static methods - Stack Overflow

I'm creating a factorybuilder class that returns a generic type:instance = ClientsFactory[SomeCl

I'm creating a factory/builder class that returns a generic type:

instance = ClientsFactory[SomeClass].new(:name).build

but I also want to have a static method to call it directly:

instance = ClientsFactory[SomeClass].build(:name)

Problem is, the latter doesn't accept the type annotation, giving me the following error:

Call to method build on ClientsFactory[SomeClass] mistakes a type for a value

Is there a way to use Generics with static methods to return a dynamic type?

I'm creating a factory/builder class that returns a generic type:

instance = ClientsFactory[SomeClass].new(:name).build

but I also want to have a static method to call it directly:

instance = ClientsFactory[SomeClass].build(:name)

Problem is, the latter doesn't accept the type annotation, giving me the following error:

Call to method build on ClientsFactory[SomeClass] mistakes a type for a value

Is there a way to use Generics with static methods to return a dynamic type?

Share Improve this question asked Feb 4 at 18:15 paulodiovanipaulodiovani 1,3052 gold badges16 silver badges35 bronze badges 1
  • 1 See also: stackoverflow/q/62281301/3141234 – Alexander Commented Feb 5 at 14:31
Add a comment  | 

1 Answer 1

Reset to default 0

You would need to make your method generic as well:

Sorbet.run

# typed: true

class ClientsFactory
  extend T::Generic
  extend T::Sig

  Result = type_member

  class << self
    extend T::Sig
    sig do
      type_parameters(:T)
        .params(type: T.type_parameter(:T), sym: Symbol)
        .returns(ClientsFactory[T.type_parameter(:T)])
    end
    def build(type, sym)
      new # dummy implementation
    end
  end
end

instance = ClientsFactory.build(String, :name)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745240018a4618125.html

相关推荐

  • ruby - Using Sorbet Generics with static methods - Stack Overflow

    I'm creating a factorybuilder class that returns a generic type:instance = ClientsFactory[SomeCl

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信