I would like to tell the compiler that if a function returns, one of its parameters is of a given type.
Contracts seems to give me that power, but does not work when I want to signal an otherwise erased type.
@ExperimentalContracts
fun <T> List<T>.isListOfInt(): Boolean {
contract {
returns(true) implies (this@isListOfInt is List<Int>)
}
return TODO("some way that I know")
}
This fails because this@isListOfInt is List<Int>
does not compile - Cannot check for instance of erased type: List<Int>
.
I understand that the current contracts specification is a Kotlin DSL, and so subject to this restriction, but at the same time being able to imply an otherwise erased type would be tremendously powerful.
Is there any way to achieve this effect now, or is it likely to be possible in future contracts implementations?