[swift-evolution] Proposal: Always flatten the single element tuple

Gwendal Roué gwendal.roue at gmail.com
Wed Jun 7 23:07:26 CDT 2017


> Le 8 juin 2017 à 05:15, Susan Cheng via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> Just a thought
> 
> if parentheses is important, why the tuples are not?
> 
> var tuple1: (Int, Int) = (0, 0)
> var tuple2: ((((Int, Int)))) = (0, 0)
> 
> type(of: tuple1) == type(of: tuple2)    // true
> 
> var void: ((((((())))))) = ()
> 
> type(of: void) == type(of: Void())  // true

I think is is because Swift doesn't have tuples with a single value: those parenthesis are just parenthesis around an expression:

    let a = 1 + 2
    let b = (1 + 2)
    let c = (1 + 2) * 3
    let d = ((1 + 2)) * 3

Many languages behave like that, Swift is no exception.

It also allows some fancy/legacy/foreign programming styles :-)

    // C-style if
    if (a && b) {
        ...
    }
    // "return function"
    return(a && b)

Languages that have single-valued tuples need a special syntax so that they are distinguished from parenthesised expressions. In Python, this is a trailing comma:

    1    # 1
    (1)  # 1
    (1,) # (1,)

Swift currently disallows trailing commas inside parenthesis.

Gwendal

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170608/7b854287/attachment.html>


More information about the swift-evolution mailing list