Casting Integer to Unit compiles successfully

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… Read More Casting Integer to Unit compiles successfully

Kotlin – How to add counts of duplicate elements in a list to generate another list

I have a simple problem but I cant seem to use predefined methods in Kotlin to be able to do this. Here is what I am trying to solve. data class A(val id: Int, val amount: Int) private List<A> generateTotal(listOfA : List<A>) The list has a couple of duplicate ids in it. For example :… Read More Kotlin – How to add counts of duplicate elements in a list to generate another list