<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">David, you can use two binary operators (or overload the same one twice if you want) to create syntax that behaves like a ternary operator.</p>
<p style="margin:0px 0px 1.2em!important">Here’s an example of using a pair of operators to interpolate between two <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">CGPoint</code> values:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248) none repeat scroll 0% 0%"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> interpolatedPoint = p1 <~~ <span class="hljs-number" style="color:rgb(0,128,128)">0.3</span> ~~> p2
</code></pre>
<p style="margin:0px 0px 1.2em!important">See <a href="https://github.com/j-h-a/Animation/blob/develop/Animation/Interpolation.swift">here</a> for the code that defines them.</p>
<p style="margin:0px 0px 1.2em!important">I went for two different operators in the end, but when experimenting I also tried using the same one, and it works fine because of overloading, for example:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248) none repeat scroll 0% 0%"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">infix</span> <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">operator</span> ~~> : <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">InterpolationPrecedence</span>
public <span class="hljs-func"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">func</span> ~~> <span class="hljs-generics"><T: Interpolatable></span><span class="hljs-params">(from: T, alpha: Double)</span> -> <span class="hljs-params">(T, Double)</span> </span>{
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> (from, alpha)
}
public <span class="hljs-func"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">func</span> ~~> <span class="hljs-generics"><T: Interpolatable></span><span class="hljs-params">(lhs: <span class="hljs-params">(T, Double)</span>, rhs: T)</span> -> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">T</span> </span>{
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> lerp(from: lhs.<span class="hljs-number" style="color:rgb(0,128,128)">0</span>, to: rhs, alpha: lhs.<span class="hljs-number" style="color:rgb(0,128,128)">1</span>)
}
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> interpolatedPoint = p1 ~~> <span class="hljs-number" style="color:rgb(0,128,128)">0.3</span> ~~> p2
</code></pre>
<p style="margin:0px 0px 1.2em!important">And as Anton demonstrated earlier, <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">?:</code> can be emulated the same way. The errors you would get if you omitted the second operator and third part are not as useful as they can be with <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">?:</code>. The compiler can probably do a much better job of optimising with <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">?:</code> as a special case, and it’s a common pattern regardless of syntax, so people would just write their own if it wasn’t there. So I think it makes sense to have it in the language. And if it wasn’t already there, I do think that it would be something we should add (same for <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">??</code>).</p>
<div title="MDH:PGRpdj5EYXZpZCwgeW91IGNhbiB1c2UgdHdvIGJpbmFyeSBvcGVyYXRvcnMgKG9yIG92ZXJsb2Fk
IHRoZSBzYW1lIG9uZSB0d2ljZSBpZiB5b3Ugd2FudCkgdG8gY3JlYXRlIHN5bnRheCB0aGF0IGJl
aGF2ZXMgbGlrZSBhIHRlcm5hcnkgb3BlcmF0b3IuPGJyPjxicj48L2Rpdj48ZGl2PkhlcmUncyBh
biBleGFtcGxlIG9mIHVzaW5nIGEgcGFpciBvZiBvcGVyYXRvcnMgdG8gaW50ZXJwb2xhdGUgYmV0
d2VlbiB0d28gYENHUG9pbnRgIHZhbHVlczo8YnI+PC9kaXY+PGRpdj5gYGBzd2lmdDxicj48L2Rp
dj48ZGl2PmxldCBpbnRlcnBvbGF0ZWRQb2ludCA9IHAxICZsdDt+fiAwLjMgfn4mZ3Q7IHAyPGJy
PjwvZGl2PjxkaXY+YGBgPGJyPjwvZGl2PjxkaXY+U2VlIFtoZXJlXShodHRwczovL2dpdGh1Yi5j
b20vai1oLWEvQW5pbWF0aW9uL2Jsb2IvZGV2ZWxvcC9BbmltYXRpb24vSW50ZXJwb2xhdGlvbi5z
d2lmdCkgZm9yIHRoZSBjb2Rl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B0aGUgc2Vjb25kIG9wZXJhdG9yIGFuZCB0aGlyZCBwYXJ0IGFyZSBub3QgYXMgdXNl
ZnVsIGFzIHRoZXkgY2FuIGJlIHdpdGggYD86YC4gVGhlIGNvbXBpbGVyIGNhbiBwcm9iYWJseSBk
byBhIG11Y2ggYmV0dGVyIGpvYiBvZiBvcHRpbWlzaW5nIHdpdGggYD86YCBhcyBhIHNwZWNpYWwg
Y2FzZSwgYW5kIGl0J3MgYSBjb21tb24gcGF0dGVybiByZWdhcmRsZXNzIG9mIHN5bnRheCwgc28g
cGVvcGxlIHdvdWxkIGp1c3Qgd3JpdGUgdGhlaXIgb3duIGlmIGl0IHdhc24ndCB0aGVyZS4gU28g
SSB0aGluayBpdCBtYWtlcyBzZW5zZSB0byBoYXZlIGl0IGluIHRoZSBsYW5ndWFnZS4gQW5kIGlm
IGl0IHdhc24ndCBhbHJlYWR5IHRoZXJlLCBJIGRvIHRoaW5rIHRoYXQgaXQgd291bGQgYmUgc29t
ZXRoaW5nIHdlIHNob3VsZCBhZGQgKHNhbWUgZm9yIGA/P2ApLjxicj48YnI+PC9kaXY+" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0"></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, 27 Oct 2016 at 01:44 David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">On Oct 25, 2016, at 23:51, Charlotte Angela Tortorella via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg"><br class="gmail_msg"></div></div><div dir="auto" class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#disadvantages-of-the-ternary-operator" class="gmail_msg" target="_blank">Disadvantages of The Ternary Operator</a></div><div class="gmail_msg"><br class="gmail_msg"></div></div></blockquote></div><div dir="auto" class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">[...]</div></div></blockquote><blockquote type="cite" class="gmail_msg"><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"></div></blockquote></div><div dir="auto" class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">6. This operator is only applicable to a single type, `Bool`.</div><div class="gmail_msg"><br class="gmail_msg"></div></div></blockquote></div><div dir="auto" class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg">[...]</div></div></blockquote></div><div dir="auto" class="gmail_msg"><blockquote type="cite" class="gmail_msg"><br class="gmail_msg"></blockquote><blockquote type="cite" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><a href="https://gist.github.com/Qata/25a11c21200f1cf8f43ed78e9ffd727c#proposed-approach" class="gmail_msg" target="_blank">Proposed Approach</a></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">We should drop the ternary operator in favor of a new extension to `Bool`.</div></div></blockquote></div><div dir="auto" class="gmail_msg"><span style="background-color:rgba(255,255,255,0)" class="gmail_msg"><div class="gmail_msg"><span style="background-color:rgba(255,255,255,0)" class="gmail_msg"><br class="gmail_msg"></span></div>I'm not sure proposals should do exactly what they claim is a downside of the current approach. </span><span style="background-color:rgba(255,255,255,0)" class="gmail_msg">Especially when the downside in question is inherent to the problem being solved.</span><div class="gmail_msg"><span style="background-color:rgba(255,255,255,0)" class="gmail_msg"><br class="gmail_msg"></span></div><div class="gmail_msg"><span style="background-color:rgba(255,255,255,0)" class="gmail_msg">FWIW, the only thing I find confusing about the ternary operator is that I can't overload it. Being able to define my own ternary operators would be great, but I don't have an answer to obvious potential ambiguities.</span></div><div class="gmail_msg"><span style="background-color:rgba(255,255,255,0)" class="gmail_msg"><br class="gmail_msg"></span></div><div class="gmail_msg"><span style="background-color:rgba(255,255,255,0)" class="gmail_msg">- Dave Sweeris</span></div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>