Generic Object Creation

You can use inline functions with reified type parameters to achieve something that closest to your initial question. You can go as berserk as:

fun main(args: Array<String>) {
    CellList(10) {
        SomeCell()
    }
    
    CellList<SomeCell>(10)
}

interface Cell { }

class SomeCell : Cell { }

class CellList<T: Cell>(val num: Int, factory : (Int) -> T) {
    companion object {
        inline operator fun <reified T: Cell> invoke(num : Int) = CellList(num) {
            T::class.java.newInstance()
        }
    }
    private val cells: MutableList<T> = java.util.ArrayList(num)
    
    init {
        for (i in 0..num - 1) {
            cells.add(factory(i))
        }
    }
}
1 Like