Class literals in Kotlin


#1

What is the plan for class literals in Kotlin?  We use class literals often in our code, and using "javaClass<String>()" in Kotlin is more verbose than "String.class".

Thanks, we really like Kotlin, and have been using it in production for about a year.  

  • Matt

#2

Most likely, it will be String::class


#3

It is more a matter of taste than an issue, but I don't like the "::" syntax. It reminds me of C++ (not the best association).

What’s wrong with String.class? Or the Scala way: classOf<String> as operator. The latter would be more consistent, since the class is not an object on which you can call methods (this class object is that returned by “.class” oder “::class”).


#4

I think angle brackets for generics, as well as curly braces for blocks should be associated even more strongly with C++, but nobody seems concerned about those :)


#5

It would be great if the "::" syntax also applies to other literals produces by the compiler, for example functions: someInstance::calcMortage. I kinda like what Java 8 did in this regard.


#6

This works for classes at the moment, e.g. MyClass::myMethod, see http://kotlinlang.org/docs/reference/reflection.html


#7

I did not think of this syntax for method references when suggesting Something.class or classOf<Something> (method "references" are completely different in Scala). So I think Something::class would be fine.


#8

"String::class" is great to me.  I like the idea of "::" being used for meta programming.   It could be expanded to get information about current context, like:

  1. this::method -> returns current method, access to its name, access to its parameters
  2. this::block -> the current block as a function, maybe even access to is variables

maybe even a way to access the stack.

classOf<String> is very similar to what is in place now javaClass<String>(), I feel this obscures the class name, and is more busy then “String::class” .