https://kotlinlang.org logo
Title
x

xenoterracide

01/15/2019, 4:51 PM
what's the difference between
::javaClass
and
::class.java
s

Shawn

01/15/2019, 4:53 PM
I wrote this up back in September, not sure if it still holds true for 1.3
// Will default to Foo's companion class if there is one
// otherwise it complains
// "Classifier 'Foo' does not have a companion object and thus must be initialized here"
Foo

// KClass<Foo>
Foo::class

// java.lang.Class<Foo>
Foo::class.java

// don't use this.
// yields some kinda generic `Class<T>` that isn't useful for comparison
Foo::javaClass

// yields the same thing as Foo::class.java, use this on instances
// to match against the class type if you need to
foo.javaClass
// e.g.
when (foo.javaClass) {
  Foo::class.java -> { ... }
}
👍 1
compiled from a few discussions on here and a bit of testing on my part
h

hudsonb

01/15/2019, 5:05 PM
+1 for avoiding
javaClass
, what it resolves to can change depending on class usage
For example I was using
javaClass.getResourceAsStream
in a UI class and it was working fine. When I converted that UI class into a
Fragment
in TornadoFX suddenly none of my resources can be found.
class.java
works in both cases.
n

nfrankel

01/15/2019, 6:01 PM
thanks i learned something today