2

What's the underlying type for Tuple in Swift? I see one mention of Tuple in the Swift module but I can't any of the following:

var x: Tuple
var y: Optional<Tuple>

Is Tuple a compiler magic or an actual type in Swift?

Boon
  • 40,656
  • 60
  • 209
  • 315
  • from _The Basics_: _"Tuples are useful for temporary groups of related values. (...) If your data structure is likely to persist beyond a temporary scope, model it as a class or structure (...)"_ https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html, you are welcome! – holex Jun 26 '14 at 13:05

4 Answers4

8

Declaring

let x: Tuple

doesn't make sense for two reasons:

  • the tuple cardinality is missing (see below)
  • the parametric types are missing

It's pretty much as declaring

let x: Array

which is forbidden as well (the parametric type is missing)

The difference here is also that Tuple is not a type per se. A tuple type is defined by both the types of the elements it holds and their number.

So a Tuple type doesn't make sense, rather you will have several types Tuple2, Tuple3 and so on. Each TupleX has have X type parameters, which will define the concrete type once provided.

In swift the compiler creates this types for you (I have no references for that, but it's the only reasonable assumption I can make), probably up to a finite number (which I still haven't found). The naming of this types doesn't look to be explicit, so you won't find a Tuple2, but instead you'll have a ($T1, $T2) type.

To sum it up, declaring a Tuple parameter is meaningless for several reasons, but you can surely have something like

var x: (Int, String)

or

var y: Optional<(Int, String)>

and so on.

To further clarify my point, here's why Tuple2 and Tuple3 need to be two different types:

class Tuple2<T1,T2> {
    init (_ e1: T1, _ e2: T2) {}
}

class Tuple3<T1,T2,T3> {
    init (_ e1: T1, _ e2: T2, _ e3: T3) {}
}

Tuple2(1, "string")
Tuple3(1, "string", ["an", "array"])

This is probably similar to what the compiler generates, and you can easily see that each TupleX takes a different number of type parameters, hence we need to define a type for each X.

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • Great answer, sums up what I could not put into words. Scala supports Tuples up to 22 elements, I wonder what's the limit in Swift. Of course there's no physical limit, only thing you'd need to do is declare more classes for each size. – Kaan Dedeoglu Jun 26 '14 at 15:14
  • @KaanDedeoglu I wondered the same after answering this question: see [here](http://stackoverflow.com/questions/24432646/what-is-the-limit-if-any-to-the-tuple-cardinality-in-swift) ;) – Gabriele Petronella Jun 26 '14 at 15:20
  • 1
    "A tuple type is defined by both the types of the elements it holds and their cardinality". It's not the cardinality of the types (which makes no sense) but the cardinality of the tuple itself. I'd write: "A tuple type is defined by both the types and the number of the elements it holds" – Analog File Jun 26 '14 at 16:56
  • "rather you will have several types Tuple2, Tuple3 and so on". More correctly those are not types, they are parametric (polymorphic) type constructors. `Array` and `Optional` are other examples of these. They are not types. They are type constructors. – Analog File Jun 26 '14 at 16:59
  • @AnalogFile I agree on the 'cardinality' (it's a matter of phrasing, and English is not my native language). But I disagree on the types: `Optional` is indeed a (polymorphic) type, as well as `Tuple2`. A type constructor is something that *instantiates* a type, producing a concrete type. In Haskel, for instance, `Just` is a unary type constructor of the polymorphic `Maybe` type. – Gabriele Petronella Jun 26 '14 at 22:59
  • @GabrielePetronella Nope. `Optional` is a type constructor. Since you know some Haskell, let's talk Haskell. `Maybe t` is a polymorphic type. `Maybe` is it's type constructor. `Just` is (one of) it's value constructor(s). In Haskell a value constructor is also a value level function despite the fact that it's capitalized (any other alphabetical value level identifier starts with a lowercase letter). For tuples things get weirder (in Haskell) because of specialized syntax: `(,)`, as well as `(,,)` etc., are both type constructors and value constructors, depending on context. – Analog File Jun 26 '14 at 23:55
  • Ok, I expressed myself wrong. As `Maybe t` is a polymorphic type, `Optional` is the same thing. In my answer I refer to `TupleX` as a type, as a shorthand for saying that `TupleX` is a type. I think it's easily deductible from the context and that highlighting the difference between `Tuple` as type constructor and `Tuple` as a type is nitpicking, apart from worthless to the core meaning of the answer. – Gabriele Petronella Jun 27 '14 at 00:00
0

According to Apple's Swift book: Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.

Narendar Singh Saini
  • 3,555
  • 1
  • 17
  • 17
0

I think it's like Anonymous Types in .net

For example, in C# you can do the following

var v = new { Amount = 108, Message = "Hello" }

just like in a Swift Tuple.

Therefore, I think it's much likely a compiler magic.

Teejay
  • 7,210
  • 10
  • 45
  • 76
  • I haven't my Mac here, can you really write: `var x:Tuple` or `var y: Optional` without assigning them? For what I learnt in the past 2 weeks, you can't... – Teejay Jun 26 '14 at 12:14
  • Can't write Optional. – Boon Jun 26 '14 at 12:17
  • And `var x:Tuple` ? Can you? – Teejay Jun 26 '14 at 12:18
  • Nope can't do this either. – Boon Jun 26 '14 at 12:18
  • So I think it's definitely a compiler magic. It just creates an anonymous struct for your tuple. In the first pages of the language guide, it's written that you should only use tuples as return types or in a function-scope, you shouldn't pass them around as data-containers, we have structs and classes for that. That's because it probably creates a new anonymous struct for each tuple you declare, so it's not much optimized. – Teejay Jun 26 '14 at 12:21
0

This is just an educated guess, but what happens is probably the compiler treating tuples as instances of generic tuple classes (or structs). That is how Scala handles tuples.

Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41