[swift-evolution] [Pitch] Replace the ternary operator with an in-language function

David Sweeris davesweeris at mac.com
Thu Oct 27 03:23:18 CDT 2016


> On Oct 26, 2016, at 21:29, Jay Abbott <jay at abbott.me.uk> wrote:
> 
> 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.
> 
Oh I know, it's just kinda clunky and inefficient sometimes, if you need to wrap a value in a struct or something simply to avoid ambiguity or enforce the correct syntax. It's not quite the same thing a ternary operator, but at one point I'd added `|` as both pre and postfix operators to allow code like "let y = |x|". The left `|` had to return a `_PartialAbsValueOp` and the right one had to take the same to invalidate the "let y = |x" or "let y = x|" syntax, one of which would've otherwise worked (depending on which op actually called "abs()").

I should've been clearer that I was talking about support for "free form" operators in general, including ternary ops, so that we don't have to fake them by declaring some # of traditional pre/in/postfix operators which all then have to work together (possibly exclusively, depending on the desired syntax), and might have to pass wrapper types around solely to help explain your syntax to the compiler.

Thanks for pointing it out, though. That's an easy trick to forget about.

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161027/89f36b35/attachment.html>


More information about the swift-evolution mailing list