lateinit on primitives
# language-proposals
x
lateinit on primitives
d
This would have to be implemented through boxing
Probably
c
Or a second field in the class. Disregarding the implementation difficulty, as a language user I've been annoyed by this several times.
👍 2
d
var x: Int by Delegates.notNull()
j
that does boxing and allocates an object for the delegate
☝️ 2
d
Yes. And you can't do this without boxing.
There is no
null
for unboxed ints.
j
Sure but "just" boxing is better than boxing and a delegate instantiation
b
What's wrong with boxing primitives? We do it all the time implicitly, if I remember correctly
j
It puts allocation into a pathway where it shouldn't exist. It's why the JVM is working so hard to specialize generics over primitives.
b
I know the tradeoffs of boxing primitives, but sometimes it's necessary to get certain functionality, like
Int?
. I think
lateinit
is an acceptable place for that tradeoff, since the reason you'd want it on a primitive in the first place is because it's expensive to calculate, so adding a small overhead for boxing it would be negligible
j
That's fine, but my comparison was against using the boxing+delegate vs. just boxing
x
yeah, my alternative to using lateinit in this case is simply to make the primitive nullable thus boxing anyways, then I have to deref with !! seems like I’m doing the same thing, but it’s less consistent syntactically from everything else around it
🤷🏼 1