https://kotlinlang.org logo
Title
w

wei

09/11/2018, 1:49 AM
I’ve got a question about comparing two enum values of the same schema but of different package path. They were both generated with avro. In my code, I can compare them like so:
val a = com.mycompany.path1.MyEnum.GOOD
val b = com.mycompany.path2.MyEnum.GOOD
if (a == b) {
    println("a == b")
}
This code actually compiles and runs. However, when I created 2 different enum classes by hand and put them into 2 paths, the code won’t compile and it will say something like:
Operator '==' cannot be applied to 'com.mycompany.path1.myEnum' and 'com.mycompany.path2.MyEnum'
Why?
s

Shawn

09/11/2018, 1:51 AM
do you see any JVM annotations on the avro-generated classes?
w

wei

09/11/2018, 1:52 AM
all I see is:
/**
 * Autogenerated by Avro
 * 
 * DO NOT EDIT DIRECTLY
 */
package com.mycompany.path1 (or path2)
public enum MyEnum {
    GOOD, BAD  ;
    ...
}
d

Dico

09/11/2018, 4:01 AM
In your first example, is there a chance either of the properties' types is
Any
?
i

ilya.gorbunov

09/11/2018, 4:26 AM
It's a known problem: https://youtrack.jetbrains.com/issue/KT-22043 By the way, @wei, does your code print "a == b" ever?
w

wei

09/11/2018, 5:26 AM
So the backstory of this is that this happened in our production code and the code compiled and ran. From our logging, we saw that indeed the condition test of
==
returned true for some cases (it’s a filter operation that we do based on the conditional). However, when I ran this locally in a test, it returned false (in other words, did not print
a == b
).
sorry guys bumping this to see if anyone has an explanation for what I observed.
r

Ruckus

09/11/2018, 6:03 PM
Did you not see @ilya.gorbunov's comment?
👆 1
w

wei

09/11/2018, 7:00 PM
The reason I’m still asking is because that example that @ilya.gorbunov gave did not coincide with what I observed. Here’s a screenshot of an example in intellij ^^
BTW, I’m using kotlin 1.2.60 . The behavior is that the compiler will complain if I defined the enum classes. It did not complain for the avro generated enums. Why?
OK. I found out that the difference was that the avro generated enum classes were in java and any other java enums won’t cause the error either. Only kotlin enum classes will error. Thanks for the help. cc @sandeep