[swift-users] Fwd: Workaround for generics not currently supporting conditional conformance to a protocol

Howard Lovatt howard.lovatt at gmail.com
Wed Nov 16 01:20:47 CST 2016


@Dave,

How do I write that though.

I can't write:

    extension Array: Equatable {
        static func ==(lhs: Array, rhs: Array) -> Bool {
            let size = lhs.count
            precondition(rhs.count == size, "The arrays must be the same
length")
            for i in 0 ..< size {
                if (lhs[i] as! Equatable) != (rhs[i] as! Equatable) {
                    return false
                }
            }
            return true
        }
    }

Because I can't cast to an Equatable, because Equatable uses Self.

Am I missing something?

 -- Howard.

  -- Howard.

On 16 November 2016 at 16:35, David Sweeris <davesweeris at mac.com> wrote:

>
> > On Nov 15, 2016, at 21:39, Howard Lovatt via swift-users <
> swift-users at swift.org> wrote:
> >
> > Hi All,
> >
> > Does anyone have a good workaround for generics not currently supporting
> conditional conformance to a protocol. As stated in the Generics Manifesto
> something like this would be nice:
> >
> >   extension Array: Equatable where Element: Equatable {
> >         static func ==(lhs: Array, rhs: Array) -> Bool { ... }
> >     }
> >
> > But I would currently write a wrapper, something like:
> >
> >     struct ArrayE<T: Equatable> {
> >         var elements: [T]
> >     }
> >     extension ArrayE: Equatable {
> >         static func ==(lhs: ArrayE, rhs: ArrayE) -> Bool { ...  }
> >     }
> >
> > This can get unwieldy when there are a lot of conditional protocol
> extensions required, i.e. wrappers round wrappers.
> >
> > Is there a better way?
> >
> > Thanks for any tips,
> >
> >   -- Howard.
> > _______________________________________________
> > swift-users mailing list
> > swift-users at swift.org
> > https://lists.swift.org/mailman/listinfo/swift-users
>
> Can you make Array conform to Equatable for any T and then in the ==
> function, if T conforms to Equatable loop the Arrays to check if they're
> equal, and if it doesn't conform just return false?
>
> I mean, it's still "wrong", but at least you won't get any false positives.
>
> - Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161116/a97a9a12/attachment.html>


More information about the swift-users mailing list