Is there a way to have a `var` without backing fie...
# announcements
r
Is there a way to have a
var
without backing field where the getter returns an optional value while the setter requires a non-optional one? Or am I just blocked on using 2 functions?
d
So you want getter to return
Foo?
but setter to only accept
Foo
?
r
Yes. It’s doing some reflection behind the scene and there may not be a value at first, unless it has been set once using the setter. Being able to manually set a null value does not provide any value
d
Then you can't use one var
c
You could use
lateinit var
or add a
requireNotNull
check in the setter. Both seem a bit dirty to me though. See if you can refactor this somehow.
r
In the end I did (much) more reflection to go and find the default value that is affected to the field