[swift-evolution] ternary operator ?: suggestion
sean at fifthace.com
Tue Jan 5 10:51:35 CST 2016
>> 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.
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.
More information about the swift-evolution