For primitives, you can always force yourself to use Integer:
lazy Integer i = f();
and make sure f() never returns null. You can do something similar with
a library class (e.g., Optional) for references. So there are surely
_safe_ ways to do it, albeit ugly ones.
I kind of prefer to have boxing like this be explicit rather than
implicit; if the user thinks they're putting an `int` in their class,
I'd like to be as transparent about that as we can.
You were willing to throw on null in the reference case; that can also
be simulated by:
lazy Foo f = requireNonNull(f());
Which isn't even that ugly or expensive. So I suspect that this is less
of a problem that one might first think, but I could be wrong.
On 4/18/2018 5:59 PM, Kevin Bourrillion wrote:
For instance fields, we have a choice; use extra space in the
object to store the "already initialized" bit, or satisfy
ourselves with the trick that String does with hashCode() -- allow
redundant recomputation in the case where the initializer serves
up the default value.
I strongly suspect there isn't going to be any generally safe way to
do the latter.