<div dir="ltr">I'd rather support defining operator functions inside the type:<div><br></div><div> extension Foo: Equatable {</div><div> func ==(rhs: Foo) -> Bool {</div><div> return self.bar == rhs.bar</div><div> }</div><div> }</div><div><br></div><div>Is there any reason this wouldn't work?</div><div><br></div><div>Stephen</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 8, 2015 at 8:36 AM, David Hart via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@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 dir="auto"><div>Your proposition makes comparison code much less readable only for a small benefit at the point of definition. I'd vote against.</div><div><div class="h5"><div><br>On 08 Dec 2015, at 11:06, Nicky Gerritsen via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>
Although I like the idea, I think it should be more general, as this
same reasoning also holds for other operators.<br>
Wouldn't it be better to allow to define operators within a type? So
that we can just implement == in the type?<br>
<br>
I do not know how hard it is to implement it or if this is even
possible. Probably it is hard, because otherwise it would already
have been done?<br>
<pre cols="72">Regards,
Nicky</pre>
<div>On 12/08/2015 11:01 AM, Richard Fox via
swift-evolution wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">
<h3><span style="font-size:16px;line-height:25.6px;font-weight:normal">Hi
all,</span><br>
</h3>
<p>I would like to
propose changing the Equatable protocol to use <code>isEqual(to:Self)
-> Bool</code>, defined inside of a type to replace the
currently used operator <code>==</code> function,
which is defined outside of the type.</p>
<h3><a href="https://gist.github.com/Nadohs/308603afa65cbbfba07c#reasoning" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2;background-color:transparent" target="_blank"><span style="font-weight:normal;font-stretch:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;color:rgb(0,0,0);vertical-align:middle"></span></a>Reasoning:</h3>
<ol>
<li>Having the conforming
function defined inside of the type is more intuitive, since
in general functions required for conformance are defined
within the type. It feels like an unnecesary detail for
learners of Swift to have to stumble through.</li>
</ol>
<p>The implementation
for this would look something like this:</p>
<pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:16px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;color:rgb(51,51,51);background-color:rgb(247,247,247)"><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0px;margin:0px;border-radius:3px;word-break:normal;border:0px;display:inline;max-width:initial;overflow:initial;line-height:inherit;word-wrap:normal;background:transparent"> public protocol Equatable{
....
/// Shortcut for defining `==` function inside type definition.
@warn_unused_result
func isEqual(to:Self) -> Bool
}
@warn_unused_result
public func == <T : Equatable>(lhs: T, rhs: T) -> Bool {
return lhs.isEqual(rhs)
}
</code></pre>
<h3><a href="https://gist.github.com/Nadohs/308603afa65cbbfba07c#impact-on-existing-code" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2;background-color:transparent" target="_blank"><span style="font-weight:normal;font-stretch:normal;font-size:16px;line-height:1;font-family:octicons;display:inline-block;color:rgb(0,0,0);vertical-align:middle"></span></a>Impact
on Existing Code:</h3>
<p>This implementation
would break existing code, but could be fixed with a default
protocol extension such as:</p>
<pre style="overflow:auto;font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:16px;font-stretch:normal;line-height:1.45;padding:16px;border-radius:3px;word-wrap:normal;color:rgb(51,51,51);background-color:rgb(247,247,247)"><code style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:13.6px;padding:0px;margin:0px;border-radius:3px;word-break:normal;border:0px;display:inline;max-width:initial;overflow:initial;line-height:inherit;word-wrap:normal;background:transparent"> /// Default `isEqual` function to satisfy other types only definiting
/// `==` for equality.
public extension Equatable{
func isEqual(to:Self) -> Bool{
return self == to
}
}
</code></pre>
<p>Not
adding the default function for <code>isEqual</code> makes
more sense to me though, since it would remove any strict
requirement for Equatable conformance and leave no warning for
the loop you would create by implementing neither <code>isEqual</code> nor <code>==</code>.<br>
<br>
Regards,<br>
Rich Fox</p>
</div>
<img alt="" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important" height="1" width="1" border="0">
<br>
<fieldset></fieldset>
<br>
<pre>_______________________________________________
swift-evolution mailing list
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>
</pre>
</blockquote>
<br>
<img alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote>
<img alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div></div></div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>