smallufo
02/14/2022, 1:53 PMinline class
pool that returns same reference ?
Related link : https://kotlinlang.slack.com/archives/C0B8Q383C/p1644805562640269
Play ground : https://pl.kotl.in/uBVYwgQgO?theme=darcula
I am building a carousel-like Int , naming Three
, which restrict any Int value to 1..3
. That is Three.of(1) == Three.of(4)
, Three.of(0) == Three.of(3)
. I use a prebuild array to simulate caching , hoping they get the same reference
private val array by lazy {
arrayOf(Three(1), Three(2), Three(3))
}
But it seems it doesn't work. When comparing reference ===
, it complains Identity equality for arguments of types Three and Three is forbidden
, which is counter-intuitive .Roukanken
02/14/2022, 3:14 PMx: Int
and not inlined y: Three
how would you compare they have same identity (when original code had x === y
?)
You can't - the only reason the inlining can be consistent is that this operation is invalid. Equals operator is easy to do: x == y
in original code would just turn to x == y.value
in inlined one.Youssef Shoaib [MOD]
02/15/2022, 6:51 PMThree
class btw, it's probably best to use an enum instead with 3 valuessmallufo
02/18/2022, 1:16 AM