admin管理员组文章数量:1415684
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
onClientsFactory[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
onClientsFactory[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
1 Answer
Reset to default 0You 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)
本文标签: rubyUsing Sorbet Generics with static methodsStack Overflow
版权声明:本文标题:ruby - Using Sorbet Generics with static methods - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745240018a2649271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论