Re: Why does non-tuple variable have .0 property?

22 views
Skip to first unread message

Chris Lattner

unread,
Jul 10, 2015, 6:02:00 PM7/10/15
to Boon, swift-l...@googlegroups.com

> On Jul 10, 2015, at 11:38 AM, Boon <bo...@nanaimostudio.com> wrote:
>
>
> Why does non-tuple variable have .0 property?
>
> For example:
>
> let x = 100
> print(x.0) // this works even though x is not a tuple, or is it?

There are esoteric internal reasons for this, but it is considered a bug, not a feature. It will be corrected at some point,

-Chris

Boon Chew

unread,
Jul 10, 2015, 8:43:29 PM7/10/15
to Chris Lattner, swift-l...@googlegroups.com

Thanks for confirming this Chris.  Some mentioned that types in Swift can be automatically promoted to a one-tuple containing that type.  Is this true or there is basically no concept of single value tuple in Swift? Trying to find a definitive answer on this.

- boon

Chris Lattner

unread,
Jul 11, 2015, 12:01:23 AM7/11/15
to Boon Chew, swift-l...@googlegroups.com

On Jul 10, 2015, at 5:43 PM, Boon Chew <bo...@nanaimostudio.com> wrote:


Thanks for confirming this Chris.  Some mentioned that types in Swift can be automatically promoted to a one-tuple containing that type.  Is this true or there is basically no concept of single value tuple in Swift? Trying to find a definitive answer on this.

There is a lot of confusion on this topic, so let me try to clarify what is going on here:

"x.0” where x is a scalar value produces that scalar value, due to odd behavior involving excessive implicit conversions  between scalars and tuples.  This is a bug to be fixed.

In "let x = (y)”, x and y have the same type, because (x) is the syntax for a parenthesis (i.e., grouping) operator, not a tuple formation operator.  There is no such thing as a single-element unlabeled tuple value.

In "(foo: 42)” - which is most commonly seen in argument lists - you’re producing a single element tuple with a label for the element.  The compiler is currently trying hard to eliminate them and demote them to scalars, but does so inconsistently (which is also a bug).  That said, single-element labeled tuples are a thing.

-Chris



Fritz Anderson

unread,
Jul 11, 2015, 12:21:18 PM7/11/15
to Boon Chew, Chris Lattner, swift-l...@googlegroups.com
The manual specifically states that there is no semantic difference between 

  a = 42
and
  (a) = 42

As I recall, the book puts it as strongly as there being no such thing as a 1-tuple. 

    — F

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAMOf6Jvz5czkxsNVYXVgf%3D9nwoHuQO8zWDTBOC3mgfrVtUSwWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages