# Custom comparison operator

**URL:** <https://forums.swift.org/t/custom-comparison-operator/31227>\
**Category:** Using Swift\
**Tags:** operators\
**Created:** [November 29, 2019, 12:44pm UTC](https://forums.swift.org/t/custom-comparison-operator/31227 "2019-11-29T12:44:31Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [November 29, 2019, 4:26pm UTC](https://forums.swift.org/t/custom-comparison-operator/31227/4 "2019-11-29T16:26:36Z")

</div>

thank you guys,

> [@donny\_wals](#):
>
> First, you don't have to write `T?` , you can use `T` . It will allow you to compare `nil` because `Optional<T>` (which is the actual type for an optional, `String?` and `Optional<String>` are the same) can be used as `T` in your comparison function. If you change your code to:
> 
> ```swift
> func <> <T: Equatable>(a: T, b: T) -> Bool {
> return a != b
> }
> 
> ```

if i did so then i'd have to make all my types equatable even if they don't have to be.

class C {}

var c: C?  
c \<\> nil // Operator function '\<\>' requires that 'C' conform to 'Equatable'

i also considered having isNil / isNonNil on Optional type to make nil checks more explicit.

this is the working solution i found:

**infix**  **operator** \<\> : ComparisonPrecedence

**extension** Equatable {  
**static**  **func** \<\> (a: Self, b: Self) -\> Bool {  
**return** a != b  
}  
}

**extension** Optional {  
**static**  **func** \<\> (a: Wrapped?, b: \_OptionalNilComparisonType) -\> Bool {  
**return** a != b  
}

**static**  **func** \<\> (a: \_OptionalNilComparisonType, b: Wrapped?) -\> Bool {  
**return** a != b  
}  
}

**extension** BinaryInteger {  
**static**  **func** \<\> (a: Self, b: Other) -\> Bool **where** Other : BinaryInteger {  
**return** a != b  
}  
}

don't ask what \_OptionalNilComparisonType is... i just copied/pasted it from a similar "func !=" of Optional type.

the int functions were needed as otherwise Int(0) \<\> Int32(0) complained (and it works with the != version)

> [@donny\_wals](#):
>
> The second error `Binary operator '<>' cannot be applied to two '(Int, Int)' operands` won't go away because tuples aren't `Equatable` either. You'll need a specific overload of `<>` that accepts tuples:
> 
> ```swift
> func <> <T: Equatable>(a: (T, T), b: (T, T)) -> Bool {
> return a.0 != b.0 && a.1 != b.1
> }
> 
> ```

so tuples are not Equatable even if we can compare them? weird.

this is very unfortunate and not scalable. but in reality in my code base i've encountered only two instances of such tuple types that i compared with !=, so i just changed the comparison to ~(a == b) (~ - is my analog of !)

given these changes now the only instances of ! in my code base are those associated with something unsafe (be it optional unwrap or implicit optionals or unsafe type cast)

---

_[View the full topic](https://forums.swift.org/t/custom-comparison-operator/31227)._
