Minor question about a `MyVal.default`-like syntax

Dan Smith daniel.smith at oracle.com
Fri Jun 9 22:06:21 UTC 2023


> On Jun 2, 2023, at 2:46 PM, Kevin Bourrillion <kevinb at google.com> wrote:
> 
> This is not too important right now, but I had thoughts, so...
> 
> 
> On Thu, Jun 1, 2023 at 10:59 AM Brian Goetz <brian.goetz at oracle.com> wrote:
> 
> Users should be able to say `new Complex()` and get a default complex value.  (Maybe they can also say `Complex.default`; maybe we won't need that.)  And the same for reflection.
> 
> I think the argument for `MyVal.default` being *unnecessary* might go like this:
> 
> * either there's no implicit constructor and `MyVal.default` won't work
> * or there is, and `MyVal.default` would have to mean the same as `new MyVal()`, so what's the point?
> 
> If that's correct, there might not be a strong argument for keeping it, but I came up with a couple weak ones.
> 
> 1. Arguably, its meaning is more apparent without the reader having to dig into MyVal.java (how much does this matter, in this case?)
> 2. It *feels like* a well-known immutable value that just kind of "exists" and has no need to be constructed. A constant. In fact people might feel tempted to make such constants?

Another weak one:

3. MyVal.default should be a linkage error (or in some worlds, evaluate to 'null') if a B3 class evolves into a B2 class. 'new MyVal()' should be fully compatible—it's the same API point whether the class is B3 or B2, and the caller using this syntax is unlikely to care about its implementation.



More information about the valhalla-spec-observers mailing list