On 10/15/2026 7:29 AM, Saurav Bhattarai wrote:
Hi all,
Value classes are a good fit for small numeric-like types such as
Int16 or Half. One place they still differ from primitives is
construction: a primitive or wrapper can be written as a literal,
but a user-defined value class requires an explicit constructor call:
Int16 a = new Int16((short) 13);
I read the 2020 thread "Typed variants of primitives" (Stephen
Colebourne / John Rose):
https://mail.openjdk.org/archives/list/[email protected]/thread/I77UXUZ4VQWMWPK3QSSEDWJJS7RJAVCZ/#X4F6C3BG3MD3656Y5WPLENKWPKH3RMGK
There, conversions between user-defined primitive-like types and
int were discussed as reasonable in principle, but requiring
carefully balanced rules.
I also searched valhalla-dev for discussions of
implicit conversions, user-defined conversions, and literal
conversions. I did not find a discussion of a per-constructor
opt-in marker specifically. The closest prior art I know of is
Swift's literal protocols and C++ converting constructors.
All of this is on our radar, but we are deliberately focusing on more
fundamental issues first. But yes, if value classes are supposed to
"work like an int", operators, conversions, and literals are all aspects
of how "ints work" that we may want to consider. And I am sure everyone
wants to know about how this is going to work, because it's the cool fun
stuff (people are often less interested in how inference will work, even
though it is more fundamental.) So you will have to be patient a bit
longer. In the meantime, I highly recommend reading through JLS Ch5
(Conversions and Contexts) as background to this harder-than-it-looks
problem.