# Generics in array extension method, with Comparable constraint can't be compared

**URL:** https://talk.remobjects.com/t/generics-in-array-extension-method-with-comparable-constraint-cant-be-compared/21941
**Category:** Silver
**Created:** [May 7, 2020, 7:58pm UTC](https://talk.remobjects.com/t/generics-in-array-extension-method-with-comparable-constraint-cant-be-compared/21941 "2020-05-07T19:58:49Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ndelic](https://talk.remobjects.com/user_avatar/talk.remobjects.com/ndelic/32/526_2.png) [@ndelic](https://talk.remobjects.com/u/ndelic)
#### Post date: [May 7, 2020, 7:58pm UTC](https://talk.remobjects.com/t/generics-in-array-extension-method-with-comparable-constraint-cant-be-compared/21941/1 "2020-05-07T19:58:50Z")

</div>

Comparing two generic values with Comparable constraints causes _Type mismatch_ compile error.

```
extension Swift.Array where T: Comparable {
    func compareItems(_ anItem1: T, _ anItem2: T) -> Int {
        
        if anItem1 < anItem2 {
            return -1
        }
        if anItem1 > anItem2 {
            return 1
        }
        return 0
    }
}

```

However, it compiles if I force cast it like this:

```
if (anItem1 as! Comparable) < (anItem2 as! Comparable) {
    return -1
}

```

This isn’t limited to Comparable, if I use `where T: Int` instead, I get the same result - error unless I force cast.

In **Echoes** forced cast as `Comparable` doesn’t compile, I have to force cast both items as `Any`.

However, even though it compiles when forced cast is used, it produces run time error.

Test project attached.

Since we had a similar conversation not long ago, I just want to make sure - is it possible to use Comparable and Equatable like this in Elements? My guess would be (maybe) no, but why are both interfaces defined in RO SwiftBaseLibrary? Are they just stubs or they can also be used? If so, how can they be used? Same as in XCode Swift?

[Generics Comparable test.zip](https://talk.remobjects.com/uploads/short-url/gGLQgElQuiFEV7Vmyk3MrvoJZBz.zip) (40.3 KB)

---

<div class="post-metadata">

### Author: ![mh](https://talk.remobjects.com/user_avatar/talk.remobjects.com/mh/32/18050_2.png) [@mh](https://talk.remobjects.com/u/mh)
#### Post date: [May 9, 2020, 2:47pm UTC](https://talk.remobjects.com/t/generics-in-array-extension-method-with-comparable-constraint-cant-be-compared/21941/2 "2020-05-09T14:47:09Z")

</div>

> [@ndelic](#):
>
> Since we had a similar conversation not long ago, I just want to make sure - is it possible to use Comparable and Equatable like this in Elements? My guess would be (maybe) no,

I believe interfaces cant satisfy standard operators (yet), no.
