Replace the primitive class with a more general null safety?

Glavo zjx001202 at gmail.com
Mon Nov 28 10:10:18 UTC 2022


After I read the JEP and related documents of Project Valhalla, I doubted
the necessity of the existence of primitive classes.

It is impossible to migrate from identity class to primitive class, which
will break binary compatibility.
This is a big obstacle for third-party libraries to support Valhalla.
We can't even use the multi-release jar to make the API contain any
primitive classes, otherwise users must use the multi-release jar to
prevent crashes.

At the same time, in order to support non null types, the universal
generics draft also greatly undermines compatibility.

Although the value class is more compatible, it may not be inlined when it
is a field or array member.
Therefore, sometimes value objects are repeatedly boxed, which may have
worse performance than non value classes.
Providing both value class and primitive class may also make it more
difficult for users to choose.

I don't like the current draft very much. In particular, I am very
dissatisfied with the huge cost of providing null safety only for primitive
types.

Now I have an idea: it's time to provide generic non null types for Java.

Since the universal generics draft has decided to break compatibility, we
can go further and provide universal non null types in Java, just like
Kotlin.

Marking type as non null is a requirement of programmers for a long time,
and Kotlin's null safety is highly praised.
I think Valhalla is the best time for Java to introduce null safety.

If we do, we no longer need the primitive class. It is enough to mark the
value class as non null.
The problem that prevents us from migrating to the primitive class no
longer exists, because we don't need the primitive class and Q type anymore.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20221128/3e4baec2/attachment.htm>


More information about the valhalla-dev mailing list