# Support for type classes

**URL:** <https://discourse.dry-rb.org/t/support-for-type-classes/759>\
**Category:** dry-types\
**Created:** [April 25, 2019, 2:05pm UTC](https://discourse.dry-rb.org/t/support-for-type-classes/759 "2019-04-25T14:05:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)\
**Post date:** [April 25, 2019, 2:05pm UTC](https://discourse.dry-rb.org/t/support-for-type-classes/759/1 "2019-04-25T14:05:31Z")

</div>

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`:

```ruby
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:

```ruby
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 🙂

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [April 29, 2019, 7:49am UTC](https://discourse.dry-rb.org/t/support-for-type-classes/759/2 "2019-04-29T07:49:42Z")

</div>

I like this idea. It should be very simple to do given that all the building blocks are in place. WDYT @flash-gordon & @timriley ❓

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [May 1, 2019, 4:19am UTC](https://discourse.dry-rb.org/t/support-for-type-classes/759/3 "2019-05-01T04:19:32Z")

</div>

Something like this would be fine to add I reckon.

I wonder whether it should be a top-level thing like this “Types.Contract” idea, or whether it should actually be a constraint, e.g. `Types::Any.constrained(respond_to?: [:call])`

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [May 1, 2019, 10:55am UTC](https://discourse.dry-rb.org/t/support-for-type-classes/759/4 "2019-05-01T10:55:40Z")

</div>

> [@timriley](#):
>
> I wonder whether it should be a top-level thing like this “Types.Contract” idea, or whether it should actually be a constraint, e.g. `Types::Any.constrained(respond_to?: [:call])`

The latter. `Types.Contract` would be just a convenient type builder, like we have `Types.Constructor` etc. this would require adding `respond_to?` predicate to `dry-logic` too.

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [May 12, 2019, 2:04pm UTC](https://discourse.dry-rb.org/t/support-for-type-classes/759/5 "2019-05-12T14:04:55Z")

</div>

> <https://github.com/dry-rb/dry-types/issues/324>
