Exhaustive when support for sealed classes #KT-7606 Fixed
When on sealed can use is (both for derived classes and objects) or just comparison (only for derived objects). A pack of tests provided.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
sealed class Tree {
|
||||
object Empty: Tree()
|
||||
class Leaf(val x: Int): Tree()
|
||||
class Node(val left: Tree, val right: Tree): Tree()
|
||||
|
||||
fun max(): Int = when(this) {
|
||||
Empty -> -1
|
||||
is Leaf -> <!DEBUG_INFO_SMARTCAST!>this<!>.x
|
||||
is Node -> <!DEBUG_INFO_SMARTCAST!>this<!>.left.max()
|
||||
}
|
||||
|
||||
fun maxIsClass(): Int = <!NO_ELSE_IN_WHEN!>when<!>(this) {
|
||||
Empty -> -1
|
||||
<!NO_COMPANION_OBJECT!>Leaf<!> -> 0
|
||||
is Node -> <!DEBUG_INFO_SMARTCAST!>this<!>.left.max()
|
||||
}
|
||||
|
||||
fun maxWithElse(): Int = when(this) {
|
||||
is Leaf -> <!DEBUG_INFO_SMARTCAST!>this<!>.x
|
||||
is Node -> <!DEBUG_INFO_SMARTCAST!>this<!>.left.max()
|
||||
else -> -1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user