| ▲ | wiseowise 3 hours ago | |||||||
How can Kotlin do it? | ||||||||
| ▲ | inigyou 2 hours ago | parent | next [-] | |||||||
Probably with hacks. Did you know a final field in Java can change its value? And I'm not talking about his reflection to make it non-final. With ordinary code only, you can read a final field before it's been initialized, so it still holds its default zero value. For example "final int x = calcX();" and have calcX print the value of x, it will be zero. There's a whole bunch of specification language describing how constants aren't actually constant in specific situations. I don't know Kotlin but I assume it does the same thing: until the non-nullable field gets initialized, it holds null and violates the type system. | ||||||||
| ||||||||
| ▲ | mike_hearn 3 hours ago | parent | prev [-] | |||||||
A lot of the language rules are required to make its approach to nullability work. Hence odd keywords like "lateinit var". | ||||||||