[swift-evolution] ternary operator ?: suggestion
matthew at anandabits.com
Tue Jan 5 10:55:31 CST 2016
> On Jan 5, 2016, at 10:51 AM, Sean Heber <sean at fifthace.com> wrote:
>>> And maybe allow a special case for boolean where you can leave off the “case true:” part:
>>> let x = something ? thing() else: otherThing()
>>> And then you could more or less replace ternary with this new construct that can do even more while looking very similar and still being pretty terse and the addition of “else” in there makes the entire expression stand out a bit more than traditional ternary expressions which, I think, addresses one of the complaints there.
>> I do not like this idea. I think a case expression should follow the case statement where a corresponding keyword is used. If you want to make this more like ternary we should just allow `:` without any keyword for the default clause.
>> Your suggestion that this would replace ternary and require an `else:` instead of `:` is going to be a non-starter. You may want to read the “frequently proposed changes” document as it explains why such a change won’t happen.
> I don’t see anything in that document that says that replacing ‘:' with ‘else:' in a ternary is necessarily a nonstarter - the ternary feature would still exist, it would only be *slightly* more verbose. The document says that one that is “better enough” hasn’t been found yet but it doesn’t read to me that it is a declared fact that “better enough” will *never* happen.
The concise nature of ternary is considered one of its big advantages. I don’t think any alternatives that require 3x more characters are going to be seriously considered. But who knows, I might be wrong.
> In any case, replacing ternary isn’t really something I care about - but I do want switch-as-expression since I’ve run into that a *lot* in one of my current projects where I adopted enums quite heavily. My suggestion was only that such a construct *could* be massaged to also cover ternary and thus could be considered a single construct that solved two “problems”: changing ternary to stand out slightly more, and getting switch-as-expression at the same time.
I agree that a case expression would be a good thing. I think we are more likely to see a proposal for that adopted if it doesn’t include any changes to ternary.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution