[swift-evolution] ternary operator ?: suggestion
al_skipp at fastmail.fm
Mon Dec 14 03:52:45 CST 2015
> On 14 Dec 2015, at 08:49, Jakob Egger via swift-evolution <swift-evolution at swift.org> wrote:
> I don't see the "improved" clarity.
> Without syntax highlighting, and no indentation to help, it becomes impossible to read. Compare:
> let x = if cond then expr1 else expr2
> let x = cond ? expr1 : expr2
With syntax highlighting, it is eminently readable.
The use of ‘?’ is also associated with Optionals in Swift, other languages don’t have this association. The ‘:’ symbol has multiple uses already; separate declarations from types, separate keys and values in Dictionaries, indicates inheritance and protocol conformance…
> The former looks just like a random English sentence, the latter is obviously a ternary expression (to my trained eye)
> The ternary operator exists in very many languages, and therefore many programmers are familiar with them.
> If the purpose is to make the language easier to understand for novice programmers, maybe you could use "when" instead of "if", for example:
> let x = when cond then expr1 else expr2
> This has several advantages:
> 1) The difference between statements and expressions becomes trivial:
> - "if" is a statement
> - "when" is an expression
> 2) it becomes easier to google. "swift when" might get better results compared to "swift ?" or "swift if"
> 3) people familiar with SQL will recognise the functional WHEN expression
> 4) Of course it is even longer, but at this point, who cares.
To add one key word to the language to support ‘if’ expressions may be regarded as a misfortune; to add two looks like carelessness. (apologies to Oscar Wilde : )
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution