# Extending \`Array.of\` behaviour to \`Set\`, \`Range\` etc

**URL:** <https://discourse.dry-rb.org/t/extending-array-of-behaviour-to-set-range-etc/1896>\
**Category:** dry-types\
**Created:** [February 9, 2025, 6:32pm UTC](https://discourse.dry-rb.org/t/extending-array-of-behaviour-to-set-range-etc/1896 "2025-02-09T18:32:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doriantaylor](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/doriantaylor/32/385_2.png) [@doriantaylor](https://discourse.dry-rb.org/u/doriantaylor)\
**Post date:** [February 9, 2025, 6:32pm UTC](https://discourse.dry-rb.org/t/extending-array-of-behaviour-to-set-range-etc/1896/1 "2025-02-09T18:32:53Z")

</div>

I am in a situation where I have container types other than `Array`, and want to be able to specify a member type. I’m not sure how, or even _if_ I can just extend the existing code to do this.

Specifically, I’m looking to be able to do things like `Set.of(Symbol)` or `Range.of(Time)`. Is there any way to do this?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [February 17, 2025, 9:05pm UTC](https://discourse.dry-rb.org/t/extending-array-of-behaviour-to-set-range-etc/1896/2 "2025-02-17T21:05:45Z")

</div>

If you’re asking whether dry-types can express this today, not to my knowledge.

`Array.of` works because Arrays are first-class type objects in the system; Set and Range are not, so while you could define nominal types for them, in order to do member type-checking you would need to implement [member classes like Array has](https://github.com/dry-rb/dry-types/blob/main/lib/dry/types/array/member.rb).

But types are not that useful on their own, so in order for schemas and contracts to understand these types, you would also need to introduce [AST](https://dry-rb.org/gems/dry-schema/1.13/advanced/rule-ast/) support for them.

In other words: it would be possible to do this, but it’s high-effort.

---

<div class="post-metadata">

**Author:** ![doriantaylor](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/doriantaylor/32/385_2.png) [@doriantaylor](https://discourse.dry-rb.org/u/doriantaylor)\
**Post date:** [February 19, 2025, 2:23pm UTC](https://discourse.dry-rb.org/t/extending-array-of-behaviour-to-set-range-etc/1896/3 "2025-02-19T14:23:23Z")

</div>

I mean is it possible to subclass `Dry::Types::Array` or otherwise refactor it into something more generic (eg a `Dry::Types::Container`, `Dry::Types::Enumerable`?) that can represent an arbitrary container-like object that has members that are all of the same type (eg `Set`, `Range`…).

I’m using `dry-types` for validation/coercion on a [parameter registry](https://rubydoc.info/gems/params-registry/) module. One of the things it does is coerce composite types (I am particularly interested in sets and ranges) out of arrays of scalar values which themselves are coerced (into numbers, dates, etc) from strings. The current solution is a bit tortured, because while I _could_ use `Array.of` if what I actually wanted was an array, I can’t express this kind of thing uniformly for all “container-shaped” objects.
