Factory methods & the language model
Dan Smith
daniel.smith at oracle.com
Fri Sep 10 16:20:35 UTC 2021
> On Sep 10, 2021, at 7:56 AM, Dan Heidinga <heidinga at redhat.com> wrote:
>
> On Thu, Sep 9, 2021 at 5:32 PM Dan Smith <daniel.smith at oracle.com> wrote:
>>
>> On Sep 9, 2021, at 1:13 PM, Dan Heidinga <heidinga at redhat.com> wrote:
>>
>> but to keep the door open to having both factories and
>> constructors in identity classes, should we use a different syntax for
>> factories in primitive classes now? That way factories would be
>> "spelled" consistently between primitive and identity classes. Doing
>> so diminishes the "codes like a class" story but leaves the door open
>> for more compatibility in the future.
>>
>>
>> Enthusiastic +1.
>>
>> I don't really *want* to do that, but if we think that's where we're headed, it is pretty weird that, say, a factory declaration in an Java interface declaration looks completely different from a factory declaration in a Java primitive class declaration. Or maybe both styles of declaration are supported by primitive classes? And does reflection treat them differently, too? Not sure if this leads anywhere good, but I want to do a bit of thinking through the implications...
>>
>
> Do you want to tackle this on list or wait for the next EG meeting?
> If you have a model / syntax in mind, we can start to work through the
> implications. Otherwise, we can all pull out the bikeshed paint....
>
Both are fine. :-)
I'm not particularly interested in settling on a bikeshed color, but am interested in the general mood for pursuing this direction at all. (And not necessarily right away, just—is this a direction we think we'll be going?)
A few observations/questions:
- 'new Foo()' traditionally guarantees fresh instance creation for identity classes. Primitive classes relax this, since of course there is no unique identity to talk about. Would we be happy with a language that relaxes this further, such that 'new Foo()' can return an arbitrary Foo instance (or maybe even null)? Or would we want to pursue a different, factory-specific invocation syntax? (And if so, should primitive classes use it too?)
- The JVM's factory methods are unnamed, but in practice it's often useful to give your factory methods names. Of course the Java language already supports *named* factory methods. Does an unnamed factory method feature significantly improve the language?
- Identity classes don't have a 'withfield' operation, which means we can't mimic the declaration syntax of primitive classes in identity class factories. Instead, identity class factories probably look just like normal static methods. Can we try to make primitive class factories *also* look like normal static methods? (Would require a 'withfield' Java expression. Honestly not sure I want to write that code.)
My initial sense: no, trying to generalize like this isn't useful, primitive class constructors are best modeled as real constructors, even though they use the factory method JVM encoding. It's not particularly likely that an identity class factory feature will be worthwhile at all, but if it is, identity and primitive classes may use a similar JVM encoding but we shouldn't view these as similar language features.
More information about the valhalla-spec-observers
mailing list