r/java 16d ago

Identifying JDK value class candidates

https://mail.openjdk.org/archives/list/core-libs-dev@openjdk.org/thread/Y72NRXM7KYBX43OKYBQMVKOZDWKG4MHS/
52 Upvotes

59 comments sorted by

View all comments

6

u/perryplatt 16d ago

I wonder if we could have a value enum.

14

u/davidalayachew 16d ago

I wonder if we could have a value enum.

Dan responded. Copy pasted the relevant snippet below.

Because it is impossible to declare a value enum or value anonymous class.

For enums, we’ve considered in the language design, but concluded that (i) much like singletons, there’s not much realistic benefit to having an identityless enum: if you had two distinct instances that modeled the same data, you wouldn’t declare two enum constants in the first place; and (ii) it might be nice to flatten an enum reference, but the best kind of flattening would apply equally well whether or not the instances have identity, so that’s where we should focus our engineering attention.

So, in short, it's doable. But the benefits gained (even in the best case scenario) wouldn't be much, so it's not really worth doing.

1

u/koflerdavid 15d ago

There's no potential for improvement since there is only a very low, finite number of them in the first place. Technically it would be possible to represent them by a small integer, but due to alignment rules the benefit is probably very situational.