<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 18, 2016, at 4:06 PM, Tony Allevato &lt;<a href="mailto:allevato@google.com" class="">allevato@google.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, May 18, 2016 at 1:56 PM Matthew Johnson via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><div class=""><br class=""></div></div></div><div style="word-wrap:break-word" class=""><div class="">Personally, I find Rust’s approach a bit ugly.&nbsp; And Tony makes a very good point in the proposal that using words requires us to learn the word associated with each operator. &nbsp;</div></div></blockquote><div class=""><br class=""></div><div class="">Right—and in addition to the cognitive overload of knowing (and remembering) the word associated with each operator, it introduced bloat in those interfaces. End users of such an interface may question why there is a named method for that operator, and whether the named method and the operator function differently.</div><div class=""><br class=""></div><div class="">Likewise, in many cases (such as arithmetic operations), it seems contrived to come up with names for an operator where that operator is already a term of art that can express the idea better than the words can.</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><span style="line-height:1.5" class="">I noted some concerns about this proposal not including automatic trampolines.&nbsp; However, if we are not going to be able to make a breaking change like this in the standard library after Swift 3 I do think it is important to give this proposal significant consideration even without them.&nbsp; Automatic trampolines can be added later but we may not have the opportunity to fix the standard library protocols later.</span></div></div></blockquote><div class=""><br class=""></div><div class="">I wish I had been able to keep automatic trampolines in—I thought it was the "killer feature" that brings the whole proposal together. (Hey core team, you can still change your mind! :)</div><div class=""><br class=""></div><div class="">That being said, I feel that the named-method approach is a huge step in the wrong direction and this proposal is strong enough without them to improve other existing ones, such as FloatingPoint, and would go a long way toward cleaning up the language in other extremely common cases (like anything that conforms to Equatable).</div></div></div></div></blockquote><div><br class=""></div><div>Tony (and core team), do you have any thoughts on the specific concerns I brought up?</div><div><br class=""></div><div><div>Imagine this:</div><div><br class=""></div><div>protocol&nbsp;P {&nbsp;<br class="">&nbsp; &nbsp; static&nbsp;func&nbsp;++++(lhs:&nbsp;Self, rhs:&nbsp;Self)&nbsp;-&gt;&nbsp;Self<div class="">}</div><div class="">func&nbsp;++++&nbsp;&lt;T:&nbsp;P&gt;(lhs: T, rhs: T)&nbsp;-&gt;&nbsp;T {<div class="">&nbsp; &nbsp; return&nbsp;T.++++(lhs, rhs)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">protocol Q {&nbsp;<br class="">&nbsp; &nbsp; static&nbsp;func&nbsp;++++(lhs:&nbsp;Self, rhs:&nbsp;Self)&nbsp;-&gt;&nbsp;Self<br class="">}</div><div class="">func&nbsp;++++&nbsp;&lt;T: Q&gt;(lhs: T, rhs: T)&nbsp;-&gt;&nbsp;T {<div class="">&nbsp; &nbsp; return&nbsp;T.++++(lhs, rhs)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">struct S: P, Q {</div><div class="">&nbsp; &nbsp; static&nbsp;func&nbsp;++++(lhs:&nbsp;Self, rhs:&nbsp;Self)&nbsp;-&gt;&nbsp;Self {</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; // ...</div><div class="">&nbsp; &nbsp; }</div><div class="">}</div></div><div><br class=""></div><div>let s1 = S()</div><div>let s2 = S()</div><div>let s3 = s1 ++++ s2 // compiler error, both trampolines are an equally good match, resulting in ambiguity</div><div><br class=""></div><div>// we have to add the following to resolve the ambiguity:</div><div><br class=""></div><div>func ++++(lhs: S, rhs: S)&nbsp;-&gt; S {<br class="">&nbsp; &nbsp; return S.++++(lhs, rhs)<br class="">}</div><div><br class=""></div><div>You could argue that this is a contrived example and is unlikely to happen in real code. &nbsp;My point is partly that it’s a regression from current state and partly that it is very likely to be confusing if people run into it.</div><div class=""><br class=""></div></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class=""></div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><div class=""><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_quote"><div class=""><div class=""><div class=""><br class=""></div><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">&nbsp;<br class=""></div></div></div></div><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br class="">
3) As has already been noted by many others, the suggested syntax for<br class="">
prefix/postfix operators is overcomplicated. The proposal is:<br class="">
<br class="">
// These are deprecated, of course, but used here just to serve as an<br class="">
// example.<br class="">
static prefix func ++(_ value: inout Self) -&gt; Self<br class="">
static postfix func ++(_ value: inout Self) -&gt; Self<br class="">
<br class="">
We don't need that. Since the 'operators' declared inside protocols are<br class="">
effectively just normal methods (apart from their names), we just need to<br class="">
name the parameters accordingly:<br class="">
<br class="">
static func ++(prefix value: inout Self) -&gt; Self<br class="">
static func ++(postfix value: inout Self) -&gt; Self<br class="">
<br class="">
4) I don't agree with the request to limit to static methods for the<br class="">
operator implementations.<br class="">
I support this for symmetrical binary operators like +, but there are other<br class="">
operators like += that seem to work better with members. That is, the<br class="">
proposed declaration:<br class="">
<br class="">
static func +=(_ lhs: inout Self, _ rhs: Self)<br class="">
<br class="">
is more similar to the global += operator definition, but is less clear than:<br class="">
<br class="">
mutating func +=(_ rhs: Self)<br class="">
<br class="">
this is apparent also at the call site. With the proposed syntax, one would<br class="">
need to do:<br class="">
<br class="">
func +=&lt;T: Foo&gt;(_ lhs: inout T, _ rhs: T) {<br class="">
&nbsp; &nbsp; T.+=(lhs, rhs)<br class="">
}<br class="">
<br class="">
while with a member function this would read more naturally as:<br class="">
<br class="">
func +=&lt;T: Foo&gt;(_ lhs: inout T, _ rhs: T) {<br class="">
&nbsp; &nbsp; lhs.+=(rhs)<br class="">
}<br class=""></blockquote><div class=""><br class=""></div><div class="">I considered this, but eventually settled on "everything is static" for consistency. As you mention, there's a stronger argument to be made for assignment operators to have "left hand side is the receiver" semantics than there are for standard infix operators, but from a consistency point of view (and ease of learning), I think having everything static and the signatures of the static operators matching those of the global operators is preferable.</div></div></div></div></blockquote><div class=""><br class=""></div></div></div></div><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">I think this would better be left as a choice to the author of the protocol. There doesn't seem to be any technical reason to place this restriction.</div></div></div></div></blockquote></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">&nbsp;</div></div></div></div><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""> (Which is also why, as I mentioned in a previous reply, I would be open to dropping the prefix/postfix keyword and making it an argument label instead, in both contexts.)</div><div class=""><div class=""><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br class="">
5) the proposal mentions the open question of ambiguities between the dot<br class="">
syntax to access methods and operators whose name starts with a dot.<br class="">
This seems to be a real issue: I don't think<br class="">
<br class="">
return T....(minimum, maximum)<br class="">
<br class="">
looks any good, even if the compiler was able to parse it.<br class="">
<br class="">
However, this just means that the methods used to implement operators with<br class="">
problematic names would need to use different names. Arguably, the only<br class="">
cases where one would really want to use methods with operator names is for<br class="">
arithmetical operators. Custom operators like ... are better expressed as<br class="">
methods with more significant names.<br class=""></blockquote><div class=""><br class=""></div><div class="">If there is a strong case where an operator is better implemented as a global operator and a&nbsp;named method, this proposal still allows that, since it's not deprecating all global operator definitions. A protocol could certainly have a requirement that is a named method, and provide a global generic operator that calls it.</div><div class=""><span style="line-height:1.5" class="">&nbsp;</span><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br class="">
6) It seems somewhat arbitrary to restrict method names to match an<br class="">
operator, nor to put requirements on the function signature. I'd say there<br class="">
are two cases, either the compiler can handle a method name that uses<br class="">
special characters, or it can't. If it can't, matching an operator name<br class="">
won't help. If it can, why put limits? There could be other creative uses of<br class="">
such names, which we would be ruling out for no particular reason. This is<br class="">
something that seems better left to the author of the protocol.<br class=""></blockquote><div class=""><br class=""></div><div class="">IMO, to reduce potential confusion, I would argue that a function whose name is the same as a defined operator should conform to the requirements (such as argument count) of that operator. It's certainly worth discussion, though! With that being said, it may be easier on users to "rule something out" now and open it up later if need be, rather than to leave it open for people to use and decide it needs to be closed later.</div></div></div></div></div></div></blockquote><div class=""><br class=""></div></div></div></div><div class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">This doesn't seem different to me from having multiple functions with the same name and different signature, which Swift allows without problems. Again, I think this is a choice that the author of the protocol should make, and there doesn't seem to be any technical reason to require otherwise.<br class=""></div></div></div></div><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class="">&nbsp;</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br class="">
7) Automatic generation of trampoline functions is out of scope so I'm not<br class="">
going to talk much about it, I only want to mention that it would make sense<br class="">
to consider making such a feature as general as possible, instead of<br class="">
focusing exclusively on operators.<br class="">
<br class="">
For example, think of the common mathematical functions like sin, cos, etc.<br class="">
It could make sense to give them the same treatment as operators, declaring<br class="">
them as part of the FloatingPoint protocol but preserving the global<br class="">
functions too.<br class="">
It might even make sense to be able to create trampolines not only from<br class="">
global space to a type, but also from one type to another type, or even for<br class="">
all methods of a type (e.g. when boxing a value inside another type).<br class="">
<br class="">
&gt; * Is the problem being addressed significant enough to warrant a change to<br class="">
Swift?<br class="">
<br class="">
Absolutely. The handling of operators in protocols has been one of the worst<br class="">
pain points in my use of Swift.<br class="">
<br class="">
&gt; * Does this proposal fit well with the feel and direction of Swift?<br class="">
<br class="">
Yes; it significantly increases clarity and consistency.<br class="">
<br class="">
&gt; * If you have used other languages or libraries with a similar feature,<br class="">
how do you feel that this proposal compares to those?<br class="">
<br class="">
I only have experience with C++ operator overloading, which is much less<br class="">
advanced.<br class="">
<br class="">
&gt; * How much effort did you put into your review? A glance, a quick reading,<br class="">
or an in-depth study?<br class="">
<br class="">
An in-depth study of the proposal, and I read all the relevant threads on<br class="">
the mailing list.<span class=""><font color="#888888" class=""><br class="">
<br class="">
--<br class="">
Nicola<br class="">
<br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</font></span></blockquote></div></div></div>
</blockquote></div></div></div></div></div></blockquote></div></div>
</blockquote></div></div></div><br class=""></div></div>
<br class="">_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div></div>
</div></blockquote></div><br class=""></body></html>