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:
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:
Similarly, we can assign values through their position:
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.
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!
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.
Advanced Tuples
These tools are for projects that use tuples extensively and creatively. You can see this in our implementation of dictionaries.
DictionariesIt 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
.
As with most types, a typealiase
d tuple has an initializer (it's just syntactic sugar, but we can think of it that way).
typealias
also allows for generics!
Last updated