Union and intersection types
A union type says that a value is one of several types, and an intersection type says that it is all of them at once. Both are written with an operator and both are ordinary types: a parameter, a result, a type argument and a field may hold one. A union is the light alternative to a sealed hierarchy when the cases need no shared behaviour, and an intersection is how a method asks for two capabilities at once.
A value of one type or another
describe takes an Int | String, and the match over it needs the two branches and no wildcard. A union also comes out of a method, which is how a parse returns either the number or the text it could not read:
union.scala
def describe(value: Int | String): String = value match
case n: Int => s"number $n"
case s: String => s"text of ${s.length}"
def length(value: String | List[?]): Int = value match
case s: String => s.length
case xs: List[?] => xs.length
def parse(value: String): Int | String =
value.toIntOption.getOrElse(value)
@main def union(): Unit =
println(describe(42))
println(describe("scala"))
val values: List[Int | String] = List(1, "two", 3)
println(values.map(describe))
println(length("abc"))
println(length(List(1, 2)))
println(parse("7"))
println(parse("seven"))
scala-cli run --server=false union.scala
number 42
text of 5
List(number 1, text of 3, number 3)
3
2
7
seven
The compiler knows the members of the union, so the match is checked like a match over a sealed trait: a missing branch is a warning, and a branch for a type the union does not have is reported as an unreachable case. String | List[?] shows the wildcard in the type argument: the method needs the length of a list of anything, and the element type is not its business. getOrElse on an Option[Int] with a String fallback produces the union without a cast.
A value that is both
Named & Aged is a value with a name and an age. A parameter of that type may read both, and a class that extends both traits satisfies it:
intersection.scala
trait Named:
def name: String
trait Aged:
def age: Int
case class Person(name: String, age: Int) extends Named, Aged
def introduce(value: Named & Aged): String =
s"${value.name}, ${value.age}"
@main def intersection(): Unit =
println(introduce(Person("Ada", 36)))
val both: Named & Aged = Person("Grace", 45)
println(both.name)
println(both.age)
val mixed: List[Named & Aged] = List(Person("Alan", 41))
println(mixed.map(introduce))
scala-cli run --server=false intersection.scala
Ada, 36
Grace
45
List(Alan, 41)
An intersection is a subtype of each of its parts, so a Named & Aged can be passed to a method that wants a Named. It is also how a type parameter is given more than one bound at once, as in [A <: Named & Aged]. The difference from a union is the direction: a union is one of the types and an intersection is all of them.
Related
- Variance and bounds: the upper bound that a type parameter with two capabilities is written with.
- Sealed hierarchies: the named alternative to a union, when the cases need methods of their own.
- Option, Either and Try: the same "value or no value" written with a type instead of a union.
- Generics: the type parameter a union is written in.
Автор статьи: Arthur Isaev