javaClass<T>()


#1

I'm doing a fair bit of framework-type work and having to work with a lot of Class objects, and to be honest, I'm finding it more cumbersome than I did in Java. Is there an alternative to using javaClass<T>()? It's a verbose operator, and the fact that it has 'java' in the name suggests that it's primary purpose is for java interoperability.

The best I’ve come up with has been to add a property to a class object, which improves the syntax a bit:

class SomeObject() {   class object {   val clazz = javaClass<SomeObject>()   } }

val x = someFunction(SomeObject.clazz)


Ideally, I’d like to be able to require that the parameter be a class object, and that a class object has a way to get the Class. Something like:

// all class objects automatically implement this trait trait ClassObject<T> {   val class: Class<T> }

val x = someFunction(SomeObject)


Perhaps the class object, if not defined, would be implied, and that all class objects would automatically implement the ClassObject trait so it would be possible to differentiate them.

Any other thoughts on how to improve this?


#2

I agree that javaClass<T>() seems to be too verbose to integrate with other java libraries. Eg: ene would have to type often when creating a logger object in each class!


#3

javaClass() is indeed for Java interop only. Kotlin doesn't have its oen reflection yet, but we are working on it. The intended syntax for obtaining a KClass is Foo::class, and for java.lang.Class — Foo::class.java() or something like this