<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 Nov 13, 2016, at 1:38 PM, Андрей Володин via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi, list!<div class=""><br class=""></div><div class="">I’m thinking about proposal that would add a possibility to save some time on creating symmetrical binary operators.</div><div class=""><br class=""></div><div class="">The latest code example from my math lib:</div><div class=""><br class=""><div class=""><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="color: rgb(231, 232, 235); font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">@inline(__always)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">static</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> *(lhs: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Angle</span><span style="font-variant-ligatures: no-common-ligatures" class="">, rhs: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Float</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Angle</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Angle</span><span style="font-variant-ligatures: no-common-ligatures" class="">(lhs.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">degrees</span><span style="font-variant-ligatures: no-common-ligatures" class=""> * rhs)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code'; min-height: 17px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">@inline(__always)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">public</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">static</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> *(lhs: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Float</span><span style="font-variant-ligatures: no-common-ligatures" class="">, rhs: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Angle</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Angle</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Angle</span><span style="font-variant-ligatures: no-common-ligatures" class="">(rhs.</span><span style="font-variant-ligatures: no-common-ligatures;" class="">degrees</span><span style="font-variant-ligatures: no-common-ligatures" class=""> * lhs)</span></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div></div></div><div style="margin: 0px; font-size: 14px; line-height: normal; font-family: 'Fira Code'; color: rgb(231, 232, 235);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">The API of Angle doesn’t really matter here, what’s important is that I have to write another operator to make it symmetrical. Not so hard: 3 lines of code. It can even refer to original implementation, instead of duplicating the logic. But I think it would be nice to have something like symmetrical keyword, just like we have associativity and precedence. </div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">I also bet that all operators should be asymmetrical by default.</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Any thoughts?</div></div></div></blockquote><div><br class=""></div>I fully support adding a “@commutative” attribute or something to let the compiler know that a function can be called with its arguments out of order, but I’m thoroughly against turning it on by default. Some operations, such as matrix multiplication, aren’t even defined if you swap the arguments.</div><div><br class=""></div><div>- Dave Sweeris</div></body></html>