Transparently use native ruby types

#1

A ruby class is already a type. However, to specify it within a dry-types declaration we must wrap it in Types.Instance(). For example:

Types::Strict::Array.of(Object)["a"]
# => NoMethodError: undefined method `call_unsafe' for Object:Class
Types::Strict::Array.of(Types.Instance(Object))["a"]
# => ["a"]

Would it be too difficult to transparently swallow ruby native types? And what are your thoughts about it?

#2

We must wrap it so that it’s not ambiguous. Inferring type from a core primitive could be tricky and it could even lead to subtle bugs; furthermore, I would not like to see code like this:

attribute :foo, Float
attribute :bar, Types::Integer.constrained(gt: 0)

I understand where you’re coming from with this, but if it really bothers you, it should be trivial to add your own helper method that would cover your specific use cases.

#3

Yep, your point is very fair. I had contradictory feelings with it anyway. Thanks!

1 Like