In some languages it is possible to specify, for example in the type signature of a function, that any type is valid as long as it conforms to a contract. Sometimes it is called a type class.
This is already possible to specify with dry-types
by hand. For example, this is a type for anything that responds to #call
:
Foo = Types.Constructor(Types::Nominal::Any) do |v|
if v.respond_to?(:call)
v
else
raise NoMethodError
end
end
What do you think about supporting it in a native way? Something like:
Foo = Types.Contract([:call]) # For anything responding to #call
Foo = Types.Contract(SomeClass, [:call]) # For any children instance of `SomeClass` responding to #call
I’m interested in the first example, but supporting the second one would make it more complete.
Other possible names would be Types.Class
or Type.Duck
, I don’t mind which name