[swift-users] can we express "does not conform to a particular protocol"?

Austin Zheng austinzheng at gmail.com
Mon Jul 3 23:41:42 CDT 2017

I'm pretty sure solutions like the one you proposed are impossible, unfortunately :(. The compiler has to choose the static override at compile time, but it can't tell until runtime whether the type conforms to `Equatable` or not (because of retroactive conformance).

It's also not possible to use the as or is dynamic casting machinery to work with `Equatable`, because of the use of the `Self` type in its requirements.

A feature that the community has expressed interest in, "generalized existentials", may allow you to implement a function that does what you want.


> On Jul 3, 2017, at 5:00 PM, David Baraff via swift-users <swift-users at swift.org> wrote:
> I’m searching for the simplest way to write a function
> 	func maybeEqual<T>(_ lhs:T, _rhs:T) -> Bool
> where it returns lhs == rhs if T is equatable, and false, otherwise.
> I tried
> func maybeEqual<T>(_ lhs:T, _rhs:T) -> Bool {
>     return false
> }
> func maybeEqual<T:Equatable>(_ lhs:T, _ rhs:T) -> Bool {
>     return lhs == rhs
> }
> but that doesn’t work.  Is there a way to express a type not matching something?
> Alternately, how can I write this?
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170703/1dac24c7/attachment.html>

More information about the swift-users mailing list