0

In swift, we can use tuple like this in the repl:

 34> let person:(name:String,age:Int)=("Hello",23)
person: (name: String, age: Int) = {
  name = "Hello"
  age = 23
}
 35> person.name
$R13: String = "Hello"
 36> person.age
$R14: Int = 23

I have not explicitly defined a type for person, and a Tuple of String and Int only have getters for 0 and 1.

Then how does the compiler know that person.name and person.age are legal invocations and person.something is not?

I ask this question because I always wanted the similar thing for Scala, but in Scala you can only do it like this:

scala> val person=("Hello",23)
person: (String, Int) = (Hello,23)

scala> val (name,age)=person
name: String = Hello
age: Int = 23

I either have to use pattern match explicitly or I have to use the _1 and _2 methods.

It seems like there is no decompiler that I can utilise to get the answer by myself, so I hope someone with the knowledge could help.

Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87

2 Answers2

1

That is a tuple. In Swift, a tuple without named parametes will allow access via .0, .1, etc. If you name the parameters, then it just looks them up by name, as if it labeled each parameter. Because you did it at declaration, that tuple's elements have names, so you can call them by name. That is also why you cannot call a parameter that has not been named, such as .something. When you say you "haven't defined a type for person", you actually have; it's a tuple, defined as (String, Int).

BJ Miller
  • 1,536
  • 12
  • 16
  • does the compiler generate a type that derives from Tuple? – Cui Pengfei 崔鹏飞 Dec 15 '14 at 06:13
  • Not to my knowledge. I don't believe there is a type "Tuple", rather a tuple is more of a lightweight grouping of related values that doesn't warrant the creation of a struct, enum, or class. You can still access elements inside a tuple (whether named or via .0, .1, etc) but a tuple does not have an initializer, defined properties/methods, etc. If the compiler does generate a type derived from Tuple, I've not seen it. – BJ Miller Dec 15 '14 at 14:01
  • this is what i am guessing, since person.something gives a compile time error rather than a runtime one, i assume there must be some kind of compile time representation of (name:String,age:Int). it could be a struct or class. i don't know how to verify my assumption, is there a decompiler available that could produce swift code? – Cui Pengfei 崔鹏飞 Dec 16 '14 at 09:45
  • To my understanding, that tuple's definition _is_ the compile-time representation of your tuple. Just like a function that takes no parameters and returns nothing is represented as `() -> ()` (or, two empty tuples), there isn't a "Function" type that it inherits from. That _is_ the type. Similarly, `(String, Int) -> String` is a "function that takes String and Int params, and returns a String". The compile-time representation of your tuple contains named parameters, and looks like `(name: String, age: Int)`, rather than just `(String, Int)`. – BJ Miller Dec 16 '14 at 18:19
-1

I don't know Swift, but that doesn't look like a tuple. It looks like a record (aka struct). Fields in records have names, in tuples, they don't.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653