Scala Tuples
- val p = (5, "hello")
- val x = p(0)
-
Expressed in Java
- public class Pair<X,Y> {
- final X x;
- final Y y;
- public Pair (X x, Y y) { this.x = x; this.y = y; }
- }
- Pair<Integer, String> p = new Pair<> (5, "hello");
- int x = p.x;
Expressed in C++
- #include <tuple>
- auto p = std::make_tuple(5, std::string("hello"));
- auto x = std::get<0>(p)
-
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Structured Data
- Tuples: fixed number of heterogeneous items `(1, "hello")`
* Lists: variable number of homogeneous items<br/> `List(1, 2, 3)` or `1 :: 2 :: 3 :: Nil`
* Immutable and mutable variants
* Pattern matching to decompose structured data into its components
---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Mutability: Variable vs. Data
- Variable mutability is different from data mutability
* Java _mutable linked list_ by default
```java
List<Integer> xs = new List<> (); // mutable variable, mutable list
final List<Integer> ys = xs; // immutable variable, mutable list
xs.add (4); ys.add (5); // list is mutable through both references
xs = new List<> (); // reference is mutable
ys = new List<> (); // fails; reference is immutable
* Scala _immutable linked list_ by default
```scala
var xs = List (4, 5, 6) // mutable variable, immutable list
val ys = xs // immutable variable, immutable list
xs (1) = 7; ys (1) = 3 // fails; list is immutable
xs = List (0) // reference is mutable
ys = List () // fails; reference is immutable
```
---