I copied some Java code and Android Studio automat...
# announcements
j
I copied some Java code and Android Studio automatically converted it for me, but fit doesn’t seem to work and my grasp of generics in Kotlin isn’t strong enough to understand why. Here’s the Java:
Copy code
if (!(raw instanceof Map)) {
    ...
}
And here’s the converted Kotlin:
Copy code
if (raw !is Map<*, *>) {
   ...
}
Now the variable
raw
is actually of type
LinkedHashTreeMap
which inherits from
Map
. What am I missing?
i
What error do you get?
j
No error, it’s is true (meaning that
raw
isn’t a
Map
).
Sorry, that was important information. 🙂
i
Does
LinkedHashTreeMap
inherit from
java.util.Map
?
j
I believe so.
LinkedHashTreeMap
extends
AbstractMap
, which in turn implements
Map
.
Sorry, I didn’t realize that
LinkedHasTreeMap
wasn’t from the standard Java library. But yes,
Map
is the ultimate interface.
s
sounds like some guava tomfoolery
j
Moshi, actually.
s
ahh gotcha
j
To be specific,
raw
looks like this at runtime:
Copy code
raw = {LinkedHashTreeMap@7019}  size = 3
 0 = {LinkedHashTreeMap$Node@7026} "data" -> " size = 3"
 1 = {LinkedHashTreeMap$Node@7027} "data_type" -> "current_power"
 2 = {LinkedHashTreeMap$Node@7028} "data_id" -> "84b1a48c-44df-4e80-bfaf-ffe024846126"
i
Then there should not be difference between Kotlin an Java code. The former compiles to
INSTANCEOF java/util/Map
check and then going to the branch if the result is false.
j
Huh, now it’s working! Honestly, I sometimes think the compiler waits until I post a question to start working just so it can embarrass me. 🤦 Thanks for your help anyway!