https://kotlinlang.org logo
Title
s

Smallville7123

04/09/2019, 1:58 AM
how do i obtain an instance of a class without specifying it explicitly
fun ret() {
    class a {
        var empty: Int = 0
    }

    val f = arrayListOf<a>()
    f.add(a()) // class a is explicitly specified
}
s

serebit

04/09/2019, 2:03 AM
You can't. And that's all I'm going to say. You need to read the docs
s

Smallville7123

04/09/2019, 2:05 AM
if i do v.add(v::class.java as E) then i get a type of
class java.lang.Class
why ;-; isnt the JVM meant to be able to know what type of class X is?
i

ilya.gorbunov

04/09/2019, 2:39 AM
Read about the type erasure in JVM generics. In run time
ArrayList<a>
doesn't know its element type, so you cannot obtain it with reflection.
Why do you need to instantiate class
a
without specifying its name?
Perhaps if you need to add something to a list and you don't have anything to add, just declare the list having nullable elements and add
null
there.