[swift-evolution] ternary operator ?: suggestion

Charles Constant charles at charlesism.com
Thu Dec 17 00:09:10 CST 2015


I've never hated the ternary operator, so I wouldn't feel comfortable
arguing very strongly against it. To my eyes, in situations where it's just
a choice of true/false, I agree the suggestions in this thread are not as
nice.

My reason for suggesting the change is that it would be nice to have a one
liner for non-boolean values. Especially when the mapping isn't likely to
be reused anyplace else. I run into this a fair amount, and the inline way
I do it (via a Dict) requires a lot of verbosity (keys are likely to be
Enums and need to be explicitly declared for the Dict). Either that or I
extend an Enum, but that's also verbose when I never intend to reuse the
mapping elsewhere in my project. So this would be handy:

Eg:

let str = dir ? (
    .Png: "Export Png",
    .Jpeg: "Export Jpeg",
    .Tiff: "Save (native)",
)

let i = boo ? (
    true: 1,
    false: 0,
    nil: -1
)

Uh hang on... I guess "nil" is invalid as a tuple key. But hey! Other than
that, the syntax would have been convenient :)

Anyways I've harped on this enough. I can live without it, if others aren't
keen on it.



‚Äč
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/fd929184/attachment.html>


More information about the swift-evolution mailing list