Kotlin Generic Property

Viewed 6351

Is there a way in kotlin to create a generic property without declaring a class level generic type? Something that looks like this:

interface Generic {
    val t: T //I need this type only once, thats why I dont wanna pass in the class level

    fun <E> gMethod(e: E) { //This works, so I'm wondering if there's something similiar to properties

    }
}
4 Answers
    private val <T> List<T>.mutable: MutableList<T>
        get() = this.toMutableList()

    @Test
    fun demonstrate_genericProperty() {
        val numbers = listOf(1, 2, 3, 4)
        val mutableNumbers = numbers.mutable
        println("numbers = ${numbers}")
        println("mutableNumbers = ${mutableNumbers}")
        println("(numbers === mutableNumbers) = ${(numbers === mutableNumbers)}")
    }
Related