# Tuples

If you have a data representation that involves multiple pieces of information, you may want to construct a data type to represent it. For example, you may want to store people's names as well as their favorite movies. It would be burdensome to have all of this information stored separately because that would be a lot to manage. If our data has limited functionality you may want to use tuples! Tuples are pretty similar to tuples of other languages. OCaml programmers, think records, but you can opt to not give fields names.

We describe a tuple's type by concatenating the interior types with commas in parenthesis:

```swift
let raahi = ("Raahi", "Menon", "Grease 2")
// raahi: (String, String, String)
```

#### Elements

Elements of a tuple are, by default, accessed by their location, but not through subscription. They are fields, so you access them as you would a variable:

```swift
// raahi: { .0: String, .1: String, .2: String }
// = { .0: "Raahi", .1: "Menon", .2: "Grease 2" }

raahi.2
// "Grease 2": String
```

Similarly, we can assign values through their position:

```swift
raahi.0 = "Rocky"
// raahi: { .0: String, .1: String, .2: String }
// = { .0: "Rocky", .1: "Menon", .2: "Grease 2" }
```

Note that tuples are value types, which means there's references or pointers. This means if we assign one tuple to two locations, we will be creating two copies of the tuples. In other words, if we change one tuple, it will not change the other.

```swift
let aBasicTuple = (0, 0)
// aBasicTuple: (Int, Int) = { .0: 0, .1: 0 }

let tupleCopy = aBasicTuple
// tupleCopy: (Int, Int) = { .0: 0, .1: 0 }

aBasicTuple.0 = 1
// aBasicTuple: (Int, Int) = { .0: 1, .1: 0 }
// tupleCopy: (Int, Int) = { .0: 0, .1: 0 }
```

#### Element Naming

When tuples get particularly large and/or complex, it can be confusing as to what piece of information is being stored where–types can help, but they aren't always enough. To fix this, we can actually name our positions!

```swift
let raahi = (firstName: "Raahi", lastName: "Menon", favoriteMovie: "Grease 2")
// raahi: (firstName: String, lastName: String, favoriteMovie: String) = ...

raahi.favoriteMove
// "Grease 2": String
```

When we name our positions, the type declaration of the tuple changes, but this is cosmetic. Tuples with elements of the same type are always the same type, regardless of naming conventions.

```swift
let raahi = (firstName: "Raahi", lastName: "Menon", favoriteMovie: "Grease 2")
// raahi: (firstName: String, lastName: String, favoriteMovie: String) = ...

let raahiCopy: (String, String, String) = raahi
// raahiCopy: (String, String, String) = 
// { .0: "Raahi", .1: "Menon", .2: "Grease 2"}
```

#### Advanced Tuples

These tools are for projects that use tuples extensively and creatively. You can see this in our implementation of dictionaries.&#x20;

{% content-ref url="/pages/1vy2ltzwkW9tKAyT0o0r" %}
[Dictionaries](/resources/swift-foundations/dictionaries.md)
{% endcontent-ref %}

It can get annoying to write a tuple's type a lot. It's quite a lot of code per use of the tuple, it's hard to read, and it's is an error-prone process. We can fix all of this by renaming types using `typealias`.

```swift
typealias Person = (firstName: String, lastName: String, favoriteMovie: String)

let raahi = ("Raahi", "Menon", "Grease 2")
// raahi: Person = ...
```

As with most types, a `typealiase`d tuple has an initializer (it's just syntactic sugar, but we can think of it that way).

```swift
let raahi = Person(firstName: "Raahi", lastName: "Menon", favoriteMovie: "Grease 2")
let raahi2 = Person("Raahi", "Menon", "Grease 2")
// raahi: Person = ...

raahi == raahi2
// true: Bool
```

`typealias` also allows for generics!

```swift
typealias Element<K, V> = (key: K, value: V)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/swift-foundations/tuples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
