50 Matching generic types
The two Box cases distinguish Box<Integer> from Box<String> at runtime and expose an appropriately typed item inside each branch.
norm
class Box<T> {
T item
}
String describe(Any candidate) {
return switch candidate {
case Box<Integer> box { break "number: ${box.item}" }
case Box<String> box { break "text: ${box.item}" }
case _ { break "other" }
}
}
main() {
Any number = Box<Integer>(item: 7)
Any text = Box<String>(item: "Norm")
printLine(describe(candidate: number))
printLine(describe(candidate: text))
}Expected output:
text
number: 7
text: NormTry it: Add a Box<Boolean> candidate and predict the final branch.
Precise rules: Reference.