<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">You mean `type(of:lhs) == type(of:rhs)`?</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhao Xin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jul 4, 2017 at 8:00 AM, David Baraff via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I’m searching for the simplest way to write a function<div><span class="m_-3462782244954834222Apple-tab-span" style="white-space:pre-wrap">        </span>func maybeEqual<T>(_ lhs:T, _rhs:T) -> Bool</div><div><br></div><div>where it returns lhs == rhs if T is equatable, and false, otherwise.</div><div>I tried</div><div><br></div><div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> maybeEqual<T>(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures"> lhs:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">T</span><span style="font-variant-ligatures:no-common-ligatures">, _rhs:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">T</span><span style="font-variant-ligatures:no-common-ligatures">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo;color:rgb(186,45,162)"><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures">return</span><span style="font-variant-ligatures:no-common-ligatures;color:#000000"> </span><span style="font-variant-ligatures:no-common-ligatures">false</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> maybeEqual<T:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Equatable</span><span style="font-variant-ligatures:no-common-ligatures">>(</span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures"> lhs:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">T</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">_</span><span style="font-variant-ligatures:no-common-ligatures"> rhs:</span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">T</span><span style="font-variant-ligatures:no-common-ligatures">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> lhs == rhs</span></div><div style="margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div>but that doesn’t work. Is there a way to express a type not matching something?</div><div>Alternately, how can I write this?</div><div><br></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>