# XOR operation/type

**URL:** <https://discourse.dry-rb.org/t/xor-operation-type/1812>\
**Category:** dry-types\
**Created:** [July 11, 2024, 6:19pm UTC](https://discourse.dry-rb.org/t/xor-operation-type/1812 "2024-07-11T18:19:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![elcuervo](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/elcuervo/32/900_2.png) [@elcuervo](https://discourse.dry-rb.org/u/elcuervo)\
**Post date:** [July 11, 2024, 6:19pm UTC](https://discourse.dry-rb.org/t/xor-operation-type/1812/1 "2024-07-11T18:19:34Z")

</div>

I’ve been fiddling around with ideas trying to find how would a `xor` type would look like.

I wonder if there’s been any conversation for a `^` operator between types.  
So far, and just for fun I have this as a naive idea:

```ruby
Types = Dry.Types()

One = Types::Coercible::Symbol.enum(*%i(a b c))
Two = Types::Coercible::Symbol.enum(*%i(c d e))

XOR = Types.Instance(Dry::Types::Type).append do |type|
  type >> -> (input) do
    raise TypeError unless type.left.valid?(input) ^ type.right.valid?(input)

    type[input]
  end
end

Options = XOR[One | Two]

Options[:a]
# => :a

Options[:c]
# Dry::Types::CoercionError: TypeError

```

This whole thing stems from the idea of having one one potential path through the types so if there’s any overlap it’d fail loudly.
