New candidate JEP: 401: Primitive Objects (Preview)
Victor Nazarov
asviraspossible at gmail.com
Thu Mar 18 11:07:38 UTC 2021
Hello,
I'd like to comment a little on one aspect that I think hasn't been
explored enough for valhalla.
There is a proposal to introduce reference-favoring primitive classes.
For me it seems that this proposal prefers the state of migration over
long-term vision.
When reading the proposal I see that in the feature we will get code like:
Optional.val<User> findUser(...)
with lot's of Optional.val scattered all over the file. It seems to me that
there is no way to fix this code in future and
the result is that we will get two flavours of the code that uses Optional:
* one flavour that is inefficient and uses references
* one flavour that is efficient but ugly because of the .val noise
I think that the feature should be optimized for new code to be both
readable and efficient *without forcing old code to migrate*.
There is a quote in a JEP:
> Other than the interpretation of the class's name when used as a type, a
reference-favoring primitive class is just like any other primitive class.
I can propose a solution following the spirit of this comment.
If reference-preference is just a namespace problem then we can probably
solve it with the namespace management tools.
Java uses import statements to manage namespaces (including static-imports).
I can propose to introduce value-favoring import, like
````
import java.util.Optional.val;
````
which means that *current compilation unit* treats `Optional` as a name of
a value-projection and Optional.ref as a name of a reference-projection.
Old code will continue to use
````
import java.util.Optional;
````
which means that it treats `Optional` as a name of a reference-projection
and Optional.val as a name of a value-projection.
Gradual migration will look like more and more code start to use `import
java.util.Optional.val` instead of `import java.util.Optional`, but
this is better the scattering `.val` all over the place (the motivation is
the same as a motivation for introducing static-imports or importing
inner-classes instead of referencing them through the top-level reference).
This special import feature can be *explained* referencing existing
Java-concepts: Optional.val is like an inner-class of the Optional-class
and so we can import it, almost... but not quite.
--
Victor Nazarov
On Wed, Mar 17, 2021 at 10:50 PM <mark.reinhold at oracle.com> wrote:
> https://openjdk.java.net/jeps/401
>
> Summary: Enhance the Java object model with user-declared primitive
> objects, which are class instances that lack object identity
> and can be stored and passed directly, without object headers or
> indirections. This is a preview language and VM feature.
>
> - Mark
>
More information about the valhalla-dev
mailing list