Why does this program prints kotlin.Unit when it should fail with ClassCastException at runtime?
class Animal<T> {
}
fun <T> Animal<T>.extension(block: ()-> T){
print(block())
}
fun main(){
//(4 as Unit) //Runtime ClassCastException, OK
//Animal<String>().extension { 2+2 } //Compilation error, ok
Animal<Unit>().extension { 2+2 } // Why no ClassCastException but prints kotlin.Unit?
}
If this is not a bug, is it possible to enforce the constraint?