Fwd: no good default issue

Brian Goetz brian.goetz at oracle.com
Fri Jul 31 19:41:19 UTC 2020

Received in the -comments box.

As far as I can tell what you're suggesting, it is that, when we detect 
a field is not initialized, we initialize it for you with some sort of 
default.  But that brings us back to the main problem: what if the class 
_has no good default_?   With what do we initialize it?

A good example of such as class is a record like

     inline record Person(String firstName, String lastName) { }

If I construct a `new Person[10]`, what should the default factory stuff 
in there?  There's no good default.

Dan describes these as Bucket 2 and Bucket 3, where Bucket 2 are those 
that have a reasonable but nonzero default (e.g., an immutable List 
implementation might want to have an empty array, rather than a null), 
and Bucket 3 have no good default.  It turns out that Bucket 3 is pretty 

-------- Forwarded Message --------
Subject: 	no good default issue
Date: 	Wed, 29 Jul 2020 01:02:02 -0400
From: 	Jack Ammo <jackammo77 at gmail.com>
To: 	valhalla-spec-comments at openjdk.java.net

feel free to disregard if this doesn't make sense, but i wonder if the
definitely unassigned / definitely assigned rules coupled with an explicit
opt in default could help the situation.

if we know that a field has not been explicitly assigned, can there be a
quick check if the class opts in to a default factory (a no arg constructor
perhaps?) and then takes the slower path of calling that factory? or maybe
upon accessing a definitely unassigned field, do the check and call the
default factory? i don't know how expensive it would be to keep track of
that, but if the opt in is a no arg constructor then there can be enough
restrictions in place to only allow either throwing an exception or pass
relevant defaults to the full all arg constructor.

and if you're willing to go the default no arg constructor route, than i
also have another idea for array initialization... what if you can define a
default "array initializer" (it can sorta look like a constructor if you
squint your eyes just right) except it takes in an array with definitely
unassigned elements of the value type and must return an array with
definitely assigned elements or throw an exception. and than that class
opts in to a slower path for arrays created with the default value.

just my 2 cents.

More information about the valhalla-spec-observers mailing list