Do all functions return tuples?

Functions can return n values, where n >= 0


If a function returns 0 values, it returns an empty tuple: ()


If a function returns more than one value, it returns a tuple.


If a function returns only a single value, is it also returning a tuple? Tuples that contain a single item are essentially interchangeable with an item of that type, so saything that a function returns Int could be essentially the same as saying that a function returns (Int).


So, is it correct to say that all functions return tuples?

Answered by Vision Pro Engineer in 33626022

Parentheses around a single type are valid grouping tokens, e.g. in `(1 + 2) + 3`. This is why `let x: (foo: Int) = 5` is not valid, and `let x: (Int) = 5; x.0` is also invalid. It is also why `let x = (5)` is inferred to be of type Int, rather than (Int). From The Swift Programming Language -> Language Reference -> Types:


If there is only one element inside the parentheses, the type is simply the type of that element. For example, the type of

(Int)
is
Int
, not
(Int)
. As a result, you can name a tuple element only when the tuple type has two or more elements.


Generally speaking, the type system understands the notion of a single-element tuple only as an implementation detail in the parser, but it is not reflected in compiled code. E.g. the dynamicType of (Int) and Int is Int.Type, not (Int).Type. The reason you see (Int) in Xcode's quick help is because quick help relies on the parsed rather than compiled metadata to display types. You'll also see this happen in syntax error messages.

Yes.

Sort of. Swift doesn't allow 1-element tuples, so I'm not sure how well that translates.


For example, `let x = (5)` is exactly equivalent to `let x = 5`–they both produce an `Int`, not an `(Int)`.

Jack,


Is it really correct to say that Swift doesn't allow 1-element tuples?


This code works without a problem in Xcode playgrounds:


var x: (Int) = (5)

var y: Int = 6

y = x

var z: Int = x

x = y + z


If you option-click on x in one of the lower lines of the playground, Xcode will gladly tell you that its type is (Int), not Int. Xcode seems to think that Int and (Int) are both valid types that can be used interchangeably.


Perhaps a better question is this: Are there any named values in Swift that aren't tuples?

Accepted Answer

Parentheses around a single type are valid grouping tokens, e.g. in `(1 + 2) + 3`. This is why `let x: (foo: Int) = 5` is not valid, and `let x: (Int) = 5; x.0` is also invalid. It is also why `let x = (5)` is inferred to be of type Int, rather than (Int). From The Swift Programming Language -> Language Reference -> Types:


If there is only one element inside the parentheses, the type is simply the type of that element. For example, the type of

(Int)
is
Int
, not
(Int)
. As a result, you can name a tuple element only when the tuple type has two or more elements.


Generally speaking, the type system understands the notion of a single-element tuple only as an implementation detail in the parser, but it is not reflected in compiled code. E.g. the dynamicType of (Int) and Int is Int.Type, not (Int).Type. The reason you see (Int) in Xcode's quick help is because quick help relies on the parsed rather than compiled metadata to display types. You'll also see this happen in syntax error messages.

Interestingly the Swift 2.0 (Xcode 7 beta 4) compiler has no problem with the following code:


let arg = (x: 4)     // why is this allowed?


and it treats arg as simply an Int. You get an error if you attempt to reference it as a tuple:

let x = arg.x     // this is a compiler error: 'Int' does not have a member named 'x'


So, this behavior is consistent with the statement that Swift doesn't support single element tuples, but it is odd that the compiler even allows the first assignment with the labeled Int.

Do all functions return tuples?
 
 
Q