With the arrival of named tuples, I’m wondering if there’s any way to express more of what class hierarchies are able to express. I may be mistaken in my recollection of set theory, or am mixing nomenclature between it and type theory, but I feel that you might be able to express subtype relationships with unions? “C = A ⋃ B // merge all members of A and B”
type Person(name: String, age: Int)
type Employee = Person ⋃ (job: String, salary: Double)
val p: Person = ("norm", 40, "programmer", 100000)
First, I put ∪
as I’m not sure what the correct symbol is in Scala, if there even is one; the |
union operator does not have the same meaning of taking all elements of both “sets” but instead something like disjunction where only one part is guaranteed.
Second, even if I did try |
or &
, that breaks the initialization for named tuples entirely. For example, creating a valid named tuple type through an intersection prevents the initialization syntax.
type Foo = (a: Int, b: String, c: Boolean)
type Bar = (a: Int, b: String, c: Float)
type T = Foo & Bar // drops c
val t: T = (a = 10, b = "hello") // error Required: T
Which probably should be raised as a bug report on it’s own.
Any ideas? I think there is potential for named tuples to offer a very elegant, powerful and unifying alternative to classes.