Is a single value variable also a tuple?

20 views
Skip to first unread message

Boon

unread,
Jul 9, 2015, 11:45:43 AM7/9/15
to swift-l...@googlegroups.com

Is a single value variable also a tuple? Given that tuple is a type, how can the variable below be both an Int and a tuple?

var value:Int = 123 // or (123)
print(value.0)  // this works


Jens Alfke

unread,
Jul 9, 2015, 1:00:00 PM7/9/15
to Boon, swift-l...@googlegroups.com

On Jul 9, 2015, at 8:45 AM, Boon <bo...@nanaimostudio.com> wrote:

Is a single value variable also a tuple? Given that tuple is a type, how can the variable below be both an Int and a tuple?

IIRC a 1-tuple is equivalent to the value it contains; in other words the compiler ignores the parens when there’s only one value inside.

—Jens

Boon

unread,
Jul 9, 2015, 5:39:36 PM7/9/15
to swift-l...@googlegroups.com, bo...@nanaimostudio.com
Understand this - question is is the variable technically still a tuple? reflect() doesn't say so even though you can do this:

let x = 100
print(x.0)

lowell!

unread,
Jul 10, 2015, 6:55:54 AM7/10/15
to swift-l...@googlegroups.com

> On Jul 9, 2015, at 2:39 PM, Boon <bo...@nanaimostudio.com> wrote:
>
> Understand this - question is is the variable technically still a tuple?

No. From the language reference:

> 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).

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html#//apple_ref/doc/uid/TP40014097-CH31-ID448
Reply all
Reply to author
Forward
0 new messages