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

Zhao Xin owenzx at gmail.com
Mon Jul 3 20:11:21 CDT 2017


You mean `type(of:lhs) == type(of:rhs)`?

Zhao Xin

On Tue, Jul 4, 2017 at 8:00 AM, 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/20170704/04f941ac/attachment.html>


More information about the swift-users mailing list