Proposal: Static/final constructors for bucket-3 primitive classes.

Clement Cherlin clement.cherlin at gmail.com
Sat Dec 4 03:15:50 UTC 2021


Motivation: A concern with primitive classes (bucket 3) is that the
all-zeroes default value may be inappropriate or even invalid in some
cases. This proposal suggests a language enhancement to give primitive
class authors control over the default value of their class without,
in most cases, requiring a constructor call to create an instance.

Proposed language change:
Primitive classes can apply either the keyword "static" or the
keyword "final", but not both, to their no-argument constructor.

A "final" no-arg constructor is evaluated once, at compile time. The
constructed object is treated as a static final constant, and can be
folded as a constant, or copied verbatim whenever a default value of
that class is instantiated.

A "static" no-arg constructor is evaluated once, when the class is loaded.
The
constructed object is copied verbatim whenever a default value of that
class is instantiated.

Justification:
Presuming that non-zero default values need to exist, and we're going
to be constructing lots and lots of primitive objects and arrays of
primitive objects, it behooves us to make initialization of default
values as efficient as possible. Much of the time, there will be no
need to call a constructor / factory method, just make a copy of a
pre-existing default value (perhaps lazily).

Related work:
For classes without sensible default values, I have another proposal I
am working on to make initializing arrays of primitive objects possible
and efficient, without resorting to the all-zeroes default.

Cheers,
Clement Cherlin


More information about the valhalla-spec-comments mailing list