I would like to write something like this: ```fun ...
# getting-started
t
I would like to write something like this:
Copy code
fun 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?
m
have you tried?:
Copy code
inline 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
}
t
I tried that but the compiler complains about
this is List<T>
because
List<T>
contains an erased type