This may be the wrong channel for this, but what w...
# codingconventions
n
This may be the wrong channel for this, but what would be the best way to set up a multiple property that can be set as null, but never retrieved as null. Essentially a mutable
lazy
property that can be cleared, and the next time you go to grab it, it creates a new instance of itself, so you don’t have to scatter the code with
variable?.
everywhere, since you’ll never grab a null version of it
l
@nic Do you want a Lazy that you can reset so next get runs the initializer from scratch?
n
Yes, exactly!
k
so a val with a custom getter
which just calls the init
l
Keep in mind my version is not thread safe (which is fine for me when I use it on one thread only). Here you go: https://github.com/LouisCAD/Splitties/blob/master/preferences/src/main/java/splitties/preferences/ResettableLazy.kt
k
if you use a local
val UNINITIALIZED = Any()
you can make it work for nullable types too
n
@louiscad this looks like exactly what I was looking for, thanks so much!
@kristofdho I’m not sure I’m fully following what you’re saying?
l
@nic My version doesn't support nulls, which is what you seem to be looking for, but if you wanted to support a resettable lazy of a nullable type (that is, an initializer that could return null), you could do so by using
val UNINITIALIZED = Any()
instead of a null as the uninitialized value
n
I don’t need to necessarily null it, I just need to be able to reset it is my real concern, so this seems ideal
k
well in Louis' solution you can't have nullable types, since null equals uninitialized. and he beat me to it
n
I actually don’t want the nullability, just the ability to reset.
k
well I don't say you'd need it, just poiting out how you could generalise it to handle any case
👍🏽 2
n
Yeah, that makes perfect sense. Thanks so much to both of you
🍻 2
k
looking at how i handled a singleassign property few months back, i used
private object UNINITIALIZED
instead of
private val UNINITIALIZED = Any()
, again, not that you're needing it 😛
n
Still helpful to see my options though, so thank you!
l
@kristofdho You basically copied lazy impl from stdlib 😛 I'd expect them to move away from the single use class some time in the future. I submitted a PR which started some discussion to just use
Any()
or a string wrapper
n
Do you have a link to the PR? I’d like to follow it
l
n
Thanks!
k
@louiscad unintentionally maybe, I actually made a modified version of the singleAssign implementation of tornadoFX as it seemed pointless to add the whole library if i don't even have a UI