Tianyu Zhu
04/15/2022, 9:47 PMfun test(foo: List<Number>) {
if (foo.containsOnly<Int>()) {
val bar: List<Int> = foo
}
}
Note that I don't want to use filterIsInstance()
here.
Is it possible to write an extension function that allows for this smart-cast?MR3Y
04/16/2022, 12:21 AMinline fun <reified T: Number> List<Number>.containsOnly(): Boolean {
contract {
returns(true) implies this is List<T>
}
forEach {
if(it !is T) return false
}
return true
}
Tianyu Zhu
04/16/2022, 12:46 AMthis is List<T>
because List<T>
contains an erased type