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

David Sweeris davesweeris at mac.com
Thu Oct 27 05:30:20 CDT 2016


Oh, yeah, sorry, didn't mean to get so far OT

Sent from my iPhone

> On Oct 27, 2016, at 05:20, Jay Abbott <jay at abbott.me.uk> wrote:
> 
> David,
> 
> You make some good points in favour of explicit support for user-defined ternary operators. You're right that it's a bit clunky without. Probably a discussion for another thread though, and additive so not for phase 1. But perhaps worth a proposal for phase 2?
> 
>> On Thu, 27 Oct 2016 at 09:23 David Sweeris <davesweeris at mac.com> wrote:
>> 
>>> 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/1d2524bb/attachment.html>


More information about the swift-evolution mailing list