Why does non-tuple variable have .0 property?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Why does non-tuple variable have .0 property?

Boon Chew

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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/e0b04e0d-f624-4244-b7e9-cca7d017de5b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Chris Lattner

> On Jul 10, 2015, at 11:38 AM, Boon <[hidden email]> 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

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/BB96299F-4C43-4520-A700-7D38C7547613%40apple.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Boon Chew

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

On Fri, Jul 10, 2015 at 6:01 PM, Chris Lattner <[hidden email]> wrote:

> On Jul 10, 2015, at 11:38 AM, Boon <[hidden email]> 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


--
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 [hidden email].
To post to this group, send email to [hidden email].
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 | Threaded
Open this post in threaded view
|

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

Chris Lattner

On Jul 10, 2015, at 5:43 PM, Boon Chew <[hidden email]> 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



--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/E9CD5BE8-8E48-4EC3-B0B2-C31A006F702E%40apple.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

Fritz Anderson
In reply to this post by Boon Chew
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


On Jul 10, 2015, at 7:43 PM, Boon Chew <[hidden email]> 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.

- boon

On Fri, Jul 10, 2015 at 6:01 PM, Chris Lattner <[hidden email]> wrote:

> On Jul 10, 2015, at 11:38 AM, Boon <[hidden email]> 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


--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/B8DEDA39-A411-4110-83D0-7297DF5CB736%40manoverboard.org.
For more options, visit https://groups.google.com/d/optout.