[swift-evolution] ternary operator ?: suggestion

Alex Lew alexl.mail+swift at gmail.com
Sun Dec 6 11:06:40 CST 2015


To elaborate, the reason I like this is that I think it fits with Swift's
general pattern (no pun intended) of extending familiar C syntactic forms
to work with functional features. C's enums become Algebraic Datatypes; C's
switch statements become pattern-matching statements; C's if and for and
while statements are also updated with pattern-matching abilities.

The ternary operator is C's answer to the "if-as-an-expression" question.
So why can't a modified version be our answer to the
"pattern-matching-as-an-expression" question?

It also has the advantage of being more explicit than the original ternary
operator -- having to label the true: and false: cases makes it clearer
what you're trying to achieve.

On Sun, Dec 6, 2015 at 12:02 PM, Alex Lew <alexl.mail+swift at gmail.com>
wrote:

> What if we left the if { ...} else { ... } syntax alone (as a statement),
> and updated the ternary expression to be a more general pattern matching
> expression (closer to "switch")? Something like
>
> let x = condition ?
>    true: "Hello"
>    false: "Goodbye"
>
> let x = optionalValue ?
>    .Some(let unwrapped): "Hello, \(unwrapped)"
>    .None: "To Whom It May Concern"
>
> let myFavoriteColor = yourFavoriteColor ?
>     .Blue: .Red
>     .Green: .Blue
>     .Red: .Green
>
> let quadrant = (x, y) ?
>     let (x, y) where x < 50 && y < 50: "top left"
>     let (x, y) where x < 50 && y > 50: "bottom left"
>     let (x, y) where x > 50 && y < 50: "top right"
>     default: "bottom right"
>
> The colon comes from the fact that this is sort of a light-weight
> expression-based "switch" statement, where each branch can only contain an
> expression, not a series of statements.
>
> This is very similar to pattern matching expressions in languages like
> Haskell, ML, and Coq.
>
> On Sun, Dec 6, 2015 at 11:25 AM, Thorsten Seitz <thorsten.seitz at web.de>
> wrote:
>
>> Am 06.12.2015 um 01:28 schrieb Alex Lew via swift-evolution <
>> swift-evolution at swift.org>:
>>
>> I don't think you can just get rid of the if statement in favor of an
>> expression. You still want to be able to do this:
>>
>> if (condition) {
>>     funcWithSideEffectsThatReturnsInt()
>> } else {
>>     funcWithSideEffectsThatReturnsString()
>> }
>>
>> but that's not a valid expression (what is its type?).
>>
>>
>> That would actually be no problem if Swift’s type system would have union
>> types (Ceylon has union and intersection types which are quite awesome and
>> enable lots of nice things quite naturally, see
>> http://ceylon-lang.org/documentation/1.2/tour/types/).
>>
>> In that case the type of such an expression would just be the union of
>> both types, which is written Int | String in Ceylon.
>>
>> -Thorsten
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151206/6d869248/attachment.html>


More information about the swift-evolution mailing list