<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 18, 2016 at 10:36 PM, Matthew Johnson <span dir="ltr">&lt;<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>&gt;</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"><br><div><div><div class="h5"><blockquote type="cite"><div>On May 18, 2016, at 3:14 PM, Nicola Salmoria &lt;<a href="mailto:nicola.salmoria@gmail.com" target="_blank">nicola.salmoria@gmail.com</a>&gt; wrote:</div><br><div><br><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">On Wed, May 18, 2016 at 10:03 PM, Matthew Johnson<span> </span><span dir="ltr">&lt;<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>&gt;</span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div><div><blockquote type="cite"><div>On May 18, 2016, at 3:00 PM, Nicola Salmoria via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 18, 2016 at 8:03 PM, Tony Allevato<span> </span><span dir="ltr">&lt;<a href="mailto:allevato@google.com" target="_blank">allevato@google.com</a>&gt;</span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Wed, May 18, 2016 at 10:02 AM Nicola Salmoria via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">&gt; * What is your evaluation of the proposal?<br><br>I&#39;m generally in strong support, having long been a proponent of removing<br>operators from protocols (the first occurrence was in this thread:<br><a href="http://article.gmane.org/gmane.comp.lang.swift.evolution/7935" rel="noreferrer" target="_blank">http://article.gmane.org/gmane.comp.lang.swift.evolution/7935</a>)<br><br>I have several comments about the details of the proposal, however.<br><br>1) At the beginning, in the &quot;Proposed solution&quot; section, the proposal says<br>&quot;This document does not propose that the current way of defining operators<br>be removed or changed at this time. Rather, we describe an addition that<br>specifically provides improvements for protocol operator requirements.&quot;<br><br>Later, however, there is a &quot;Deprecation of non-static protocol operators&quot;<br>section which suggest to do exactly that, and this is reiterated in the<br>&quot;Impact on existing code&quot; section.<br><br>Since I think that the deprecation of global operator overloads is the<br>crucial point of the proposal, I assume that the former is an oversight.<br></blockquote><div><br></div></div><div dir="ltr"><div class="gmail_quote"><div>I could probably do a better job of clarifying the wording here. The proposal does *not* deprecate *all* global operator overloads. Global operators can still be implemented as they have been in Swift. So if you have a concrete type like `struct Matrix`, you can still define at the global level `func +(lhs: Matrix, rhs: Matrix) -&gt; Matrix`.</div><div><br></div><div>What&#39;s being deprecated is the current syntax used to define operator requirements inside protocols (by making the functions static) and the manner by which subtypes conform (ditto, through static methods instead of global functions).</div></div></div></div></blockquote><div><br></div><div>OK, I guess the unclear part is when you talk about &quot;an addition that specifically provides improvements for protocol operator requirements.&quot; This is not just an addition; it&#39;s intended to completely replace the protocol operator syntax.<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>2) The method signatures in the examples are not up to date with the current<br>Swift 3 syntax. For example:<br><br>protocol Equatable {<br> <span> </span>static func ==(lhs: Self, rhs: Self) -&gt; Bool<br>}<br><br>should be:<br><br>protocol Equatable {<br> <span> </span>static func ==(_ lhs: Self, _ rhs: Self) -&gt; Bool<br>}<br></blockquote><div><br></div><div>Unless I&#39;m mistaken, from looking at the Swift 3 branch of stdlib, the syntax changes don&#39;t appear to apply to operator functions. Since they are a special case that don&#39;t have argument labels, it wouldn&#39;t make sense to require them (or rather, the placeholders) here.</div></div></div></div></blockquote><div><br></div><div>I don&#39;t agree with this.<br><br></div><div>Operators are called like this:<br><br></div><div>x = y + z<br><br></div><div>Of course it doesn&#39;t make sense to have parameter labels there.<br></div><div><br></div><div>But the ones inside the protocol are not operators. They are methods, and are called like methods. They happen to have funny names, but they are still methods, and are called like this:<br><br></div><div>x = T.+(y, z)<br><br></div><div>In this case not only it makes sense for the parameters to have labels, but making them behave differently from normal methods would be inconsistent, and a step backwards from all the progress that has been made in Swift 3 on that front.<br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>3) As has already been noted by many others, the suggested syntax for<br>prefix/postfix operators is overcomplicated. The proposal is:<br><br>// These are deprecated, of course, but used here just to serve as an<br>// example.<br>static prefix func ++(_ value: inout Self) -&gt; Self<br>static postfix func ++(_ value: inout Self) -&gt; Self<br><br>We don&#39;t need that. Since the &#39;operators&#39; declared inside protocols are<br>effectively just normal methods (apart from their names), we just need to<br>name the parameters accordingly:<br><br>static func ++(prefix value: inout Self) -&gt; Self<br>static func ++(postfix value: inout Self) -&gt; Self<br><br>4) I don&#39;t agree with the request to limit to static methods for the<br>operator implementations.<br>I support this for symmetrical binary operators like +, but there are other<br>operators like += that seem to work better with members. That is, the<br>proposed declaration:<br><br>static func +=(_ lhs: inout Self, _ rhs: Self)<br><br>is more similar to the global += operator definition, but is less clear than:<br><br>mutating func +=(_ rhs: Self)<br><br>this is apparent also at the call site. With the proposed syntax, one would<br>need to do:<br><br>func +=&lt;T: Foo&gt;(_ lhs: inout T, _ rhs: T) {<br>   <span> </span>T.+=(lhs, rhs)<br>}<br><br>while with a member function this would read more naturally as:<br><br>func +=&lt;T: Foo&gt;(_ lhs: inout T, _ rhs: T) {<br>   <span> </span>lhs.+=(rhs)<br>}<br></blockquote><div><br></div><div>I considered this, but eventually settled on &quot;everything is static&quot; for consistency. As you mention, there&#39;s a stronger argument to be made for assignment operators to have &quot;left hand side is the receiver&quot; 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><br></div><div>I think this would better be left as a choice to the author of the protocol. There doesn&#39;t seem to be any technical reason to place this restriction.<br></div></div></div></div></div></blockquote><div><br></div></div></div><div>The problem with this is that people will make different choices.  This may result in boilerplate where a type has to provide both static *and* instance methods to conform to different protocols.  Better to avoid that possibility.</div></div></div></blockquote><div><br></div><div>Do you have a specific example in mind?<br></div></div></div></blockquote><div><br></div></div></div><div>No, it’s not possible today so there is no existing code to reference.  However, if you give people a choice some people will inevitably make a different choice than other people.</div></div></div></blockquote><div><br></div><div>You mentioned needing to add boilerplate to make a type conform to different protocols. In which scenario could that happen?<br></div>Once you take out the standard operators which will be taken care of by the stdlib, I can&#39;t readily think of an example of an operator which could be defined by two different protocols.<br><br></div><div class="gmail_quote">Also, this isn&#39;t a problem specific to operators; the same kind of incompatibility can happen with normal methods. We lease freedom with normal methods, so why should operators be more limited?<br></div><div class="gmail_quote"> <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span class=""><br><blockquote type="cite"><div><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><br>I expect the stdlib to illustrate what idiomatic Swift is expected to be, and people to follow suit for the standard operators.<br></div></div></div></blockquote><div><br></div></span><div>That is exactly what this proposal is attempting to do, and in a way that the language enforces the decision.  </div><span class=""><br><blockquote type="cite"><div><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><br></div><div>What benefit do you perceive in allowing operator instance methods?</div></div></div></blockquote><div><br></div><div>The fact that operators like += are more naturally implemented as an instance method than as a static method is already a clear benefit to me.<br></div></div></div></blockquote><div><br></div></span><div>Are you specifically thinking of assignment operators?  If so, I wouldn’t have a problem if the proposal specified assignment operators as instance methods and all other operators as static methods.  As long as there is only one way to define a specific operator requirement I will be happy.</div><span class=""><br><blockquote type="cite"><div><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div><br></div><div>But in general, there aren&#39;t only the standard operators; people will define custom operators and will have different needs depending on their semantics. If there isn&#39;t a technical limitation, I don&#39;t see a reason to add arbitrary restrictions.<br></div></div></div></blockquote><div><br></div></span><div>There is no difference in capability here so I’m not sure what you mean by having different “needs”.</div></div></div></blockquote><div><br></div><div>By needs I mean choosing the representation that better fits the semantics of the operator.<br></div><div>For symmetrical binary operators, a static method is probably the best choice, but for other kinds of operators it might not be.<br></div><div> <br></div><div> </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"><div><div><div class="h5"><br><blockquote type="cite"><div><div class="gmail_quote" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>(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> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>5) the proposal mentions the open question of ambiguities between the dot<br>syntax to access methods and operators whose name starts with a dot.<br>This seems to be a real issue: I don&#39;t think<br><br>return T....(minimum, maximum)<br><br>looks any good, even if the compiler was able to parse it.<br><br>However, this just means that the methods used to implement operators with<br>problematic names would need to use different names. Arguably, the only<br>cases where one would really want to use methods with operator names is for<br>arithmetical operators. Custom operators like ... are better expressed as<br>methods with more significant names.<br></blockquote><div><br></div><div>If there is a strong case where an operator is better implemented as a global operator and a named method, this proposal still allows that, since it&#39;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><span style="line-height:1.5"> </span><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>6) It seems somewhat arbitrary to restrict method names to match an<br>operator, nor to put requirements on the function signature. I&#39;d say there<br>are two cases, either the compiler can handle a method name that uses<br>special characters, or it can&#39;t. If it can&#39;t, matching an operator name<br>won&#39;t help. If it can, why put limits? There could be other creative uses of<br>such names, which we would be ruling out for no particular reason. This is<br>something that seems better left to the author of the protocol.<br></blockquote><div><br></div><div>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&#39;s certainly worth discussion, though! With that being said, it may be easier on users to &quot;rule something out&quot; 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></blockquote><div><br></div><div>This doesn&#39;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&#39;t seem to be any technical reason to require otherwise.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>7) Automatic generation of trampoline functions is out of scope so I&#39;m not<br>going to talk much about it, I only want to mention that it would make sense<br>to consider making such a feature as general as possible, instead of<br>focusing exclusively on operators.<br><br>For example, think of the common mathematical functions like sin, cos, etc.<br>It could make sense to give them the same treatment as operators, declaring<br>them as part of the FloatingPoint protocol but preserving the global<br>functions too.<br>It might even make sense to be able to create trampolines not only from<br>global space to a type, but also from one type to another type, or even for<br>all methods of a type (e.g. when boxing a value inside another type).<br><br>&gt; * Is the problem being addressed significant enough to warrant a change to<br>Swift?<br><br>Absolutely. The handling of operators in protocols has been one of the worst<br>pain points in my use of Swift.<br><br>&gt; * Does this proposal fit well with the feel and direction of Swift?<br><br>Yes; it significantly increases clarity and consistency.<br><br>&gt; * If you have used other languages or libraries with a similar feature,<br>how do you feel that this proposal compares to those?<br><br>I only have experience with C++ operator overloading, which is much less<br>advanced.<br><br>&gt; * How much effort did you put into your review? A glance, a quick reading,<br>or an in-depth study?<br><br>An in-depth study of the proposal, and I read all the relevant threads on<br>the mailing list.<span><font color="#888888"><br><br>--<br>Nicola<br><br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">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></font></span></blockquote></div></div></div></blockquote></div><br></div></div>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div></div></div></div></blockquote></div></div></blockquote></div></div></div><br></div></blockquote></div><br></div></div>