javaClass<MyClass>() not as nice as MyClass.class


#1

javaClass<MyClass>() not as nice as MyClass.class.  Any plans to add simpler static class references?  We use static class references all through out our code with our Dependency Inject engine, and Dynamic Transforms, the kotlin function is not as strait forward as the java static reference.  


#2

Once there's Kotlin reflection, the syntax most likely will be:

MyClass::class

, which will return an instance of a Kotlin class. This is work in progress right now.


#3

including

this::class

?


#4

Will the Kotlin Class be compatible with a java class?  For example will ArrayList.class == ArrayList::class?


#5

Yes, but we've yet to decide the syntax for getting a class by an instance. Allowing arbitrary expressions on the left hand side creates a problem for classes with class objects ('MyClass' can be seen both as a type symbol and as an expression)


#6

No, it'll be a separate platform-agnostic API. On JVM, we'll provide a way to get a Java class by a Kotlin class to interoperate with existing Java libraries.

But to answer your original question (which I suppose implied that you use some dependency injection library in Java): I’m afraid javaClass is the best that you can get. Hopefully some day there’ll be a dependency injection framework in Kotlin and you’ll only need to use Kotlin reflection from then on :slight_smile:


#7

Could we also have similar references for fields and methods?

Like

val fieldRef:kotlin.Field<MyClass,FieldType> = MyClass::field(“fieldName”) // compile-time check if argument is constant

val methodRef:kotlin.Method<MyClass,FunctionN<ReturnType,ArgType1,…,ArgTypeN>> = MyClass::method(“methodName”); // for overloaded methods, argument type list should be provided

val fieldVal:FieldType = fieldRef.get(myClassInstance);

fieldRef.set(myClassInstance,fieldVal);

val methodVal:ReturnType = methodRef.get(myClassInstance)(arg1,...,argN);

References to fields of object could be made like this:

data class FieldRef<C,T>(val field:Field<C,T>, val obj:C) {

  fun get() = field.get(obj);

  fun set(value:T) = field.set(obj,value);

}


#8

Already there for functions, properties are coming


#9

Is current language documentation available and maintained somewhere? I can't find anything about callable references on http://confluence.jetbrains.com/display/Kotlin/


#10

We are working on an updated docs site. The only docs available about this feature can be found here: http://blog.jetbrains.com/kotlin/2013/06/kotlin-m5-3-idea-13-delegated-properties-and-more/