Scala Tuples
- val p : (Int, String) = (5, "hello")
- val x : Int = 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;
# <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
```
---