How to compare inline classes? My usecase. I have...
# eap
p
How to compare inline classes? My usecase. I have defined 5 `Int`s and an Inline class using Following.
inline class State(val state: Int)
And in my regular class I have a variable of the State using
Delegates.observable
. Whenever I try to compare two states, I get :
State cannot be cast to java.lang.Number
My sample comparision code looks like following.
this.state==State(1)
r
Copy code
this.state == State(1).state
or
Copy code
State(this.state) == State(1)
should work. That's exactly the use case for inline classes, that you can't accidentally cross-assign semantically different values, so you have to make it explicit.
p
Copy code
State(this.state) == State(1)
Doesn’t seem to work. My variable is
Copy code
var state by Delegates.observable(State(0){
___
}
Usually state is being re-assigned. Now at some point if I compare using 1.
Copy code
state=State(0)
Or
Copy code
state.state==0
Both gives
ClassCastException
r
This compiles fine for me:
Anything I'm doing differently than you here?
p
Compilation is not an issue here. It compiled fine for me as well. How about output? Does your code runs without any
ClassCastException
?
Also, it looks like our environment is different. I use Android Studio, and probably you are in Intellij and with Java 1.8. These are not present in my build config
r
Huh, you're right, I assumed you had a compile error so I didn't try to run it. Crashes for me too.
Having a look at the compiled code it seems to me like this is an unfortunate combination of type erasure with Delegates, and the compiler messes up boxing/unboxing at some point
Definitely looks like a bug to me.
o
@mikhail.zarechenskiy ^^
m
Yes, it's a bug: https://youtrack.jetbrains.com/issue/KT-27737 We'll fix it soon, sorry for the inconvenience!
🙏 1