Civilizer - let's civilize those primitive types !

John Bossons jbossons at gmail.com
Fri Jan 13 23:15:59 UTC 2023


Agreed that the use of a .ref or .val tag is an awkward deviation from
familiar syntax.

Use of a @NotNull annotation is one alternative. A contextual keyword (e.g.
*notnull* or *nonulls*) is another.


The key thing is that, whether keyword or annotation, it should only need
to be specified when a field is defined. Every implication of a *notnull*
specification should be capable of being generated by the compiler.


Examples:


    primitive class PhoneNumber {

        // all fields are non-negative never-null integer values

    }

   value class ContactPhoneInfo {

        private notnull PhoneNumber mobile;   // equivalent to
PhoneNumber.val

        private PhoneNumber landLine;           // optional, equivalent to
PhoneNumber.ref

    }

    value class EmailContact {

        private notnull String emailAddress;     // never missing

        private ContactPhoneInfo phones;       // optional, may be missing

    }


The *notnull* modifier can be applied to any value class field type. If the
field is a primitive or value object, it signals that the field value may
be stored as a flattened value without needing an additional bit or
reference to allow for a null value. But it also has other uses, including
for identity-type fields like the String field in the above example.


For example, if a field is a *notnull*, the compiler could autogenerate
code throwing a NullPointerException (or equivalent) if a client attempts
to instantiate the value object with a null value for that field. (Or
better still, reject client code that attempts to supply an invalid null
value). If a value object field is not a *notnull*, the compiler could
autogenerate an *Optional* return value for the corresponding accessor.


The advantage of the keyword over the annotation is that it can only be
used in one place: a field definition, the only place it’s needed.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20230113/4fc7b166/attachment.htm>


More information about the valhalla-dev mailing list