Martin Nordholts
02/12/2020, 8:54 AMnull TheValue
? I expect it to print TheValue TheValue
enum class EnumClass(val self: EnumClass? = null) {
TheValue(self = TheValue)
}
enum class OtherEnumClass(val other: EnumClass? = null) {
OtherValue(other = EnumClass.TheValue)
}
fun main() {
println("${EnumClass.TheValue.self} ${OtherEnumClass.OtherValue.other}")
}
spand
02/12/2020, 8:56 AMMartin Nordholts
02/12/2020, 8:58 AMdiesieben07
02/12/2020, 9:00 AMTheValue
constructor does not run when you print, it runs at the class-init time of EnumClass
, before EnumClass.TheValue
has been assigned (how can it be assigned, you are in the middle of creating it's value!)spand
02/12/2020, 9:01 AMTheValue(self = TheValue)
TheValue has not been thus self is assigned to nullMartin Nordholts
02/12/2020, 9:01 AMStephan Schroeder
02/12/2020, 10:11 AMby lazy {..}
to compute values that aren’t available immediately).
It pretty much took extra effort (nullable class + default null parameter value) to not make the compiler complain about this 😂Martin Nordholts
02/12/2020, 10:12 AMStephan Schroeder
02/12/2020, 10:14 AMnull
to start with.Martin Nordholts
02/12/2020, 10:16 AMexactly what you defindedI don’t see how you can think that
TheValue(self = TheValue)
is to be interpreted
TheValue(self = null)
spand
02/12/2020, 10:30 AMMike
02/12/2020, 12:36 PMself
in a class to the class itself. But in order to assign it, the class has to be constructed.
But in order for the class to be constructed, it needs the pointer to the class.
etc, etc, etc...
So I'm unsure how you expect it to work.
And the above also explains why it wouldn't have compiled if self
was non-null. But just getting it to compile doesn't always guarantee it will work. We all wish it was that simple 😉Martin Nordholts
02/12/2020, 12:40 PMenum class EnumClass(var self: EnumClass? = null) {
TheValue();
}
enum class OtherEnumClass(val other: EnumClass? = null) {
OtherValue(other = EnumClass.TheValue)
}
fun main() {
EnumClass.TheValue.self = EnumClass.TheValue
println("${EnumClass.TheValue.self} ${OtherEnumClass.OtherValue.other}")
}
that code runs just fine.diesieben07
02/12/2020, 12:42 PMTheValue
at the point where you construct TheValue
, in the initializer of EnumClass
.Martin Nordholts
02/12/2020, 12:44 PMdiesieben07
02/12/2020, 12:46 PMMartin Nordholts
02/12/2020, 12:46 PMMike
02/12/2020, 12:47 PMTheValue
object gets created as soon as EnumClass.TheValue
is executed. This will initialize the object, assign it in memory. And since self
is nullable AND has a default value, all is good.
Now the compiler will execute the assignment of self
within TheValue
, and it has an object to point to. This works ONLY because enums are singletons.
When you try to combine it, you put the compiler into a stackoverflow situation as described above.
Hopefully that helps you conceptualize it.Martin Nordholts
02/12/2020, 12:50 PMMike
02/12/2020, 1:57 PMilya.gorbunov
02/12/2020, 10:18 PM