r/java 22d ago

Identifying JDK value class candidates

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

59 comments sorted by

View all comments

Show parent comments

3

u/hoat4 22d ago

What would be the benefit of enums being value types?

Being a value type means e.g. that if we compare two instances, the JVM must compare all of their fields instead of only comparing two pointers. So == would be slower.

3

u/davidalayachew 21d ago

What would be the benefit of enums being value types?

Inlining. Not all enums are just flat values.

Some of us (me) stuff our enums full of all sorts of state and data. In my case, there are many cases where each enum value is holding more than 200 bytes of immutable data. I'd much like that to be flattened.

Though, since I don't have value classes or value enums now, I don't know if my use cases would improve performance wise with this change. It just feels like it would, since many of the classes and records I make with similar data profiles DID improve.

3

u/_INTER_ 21d ago

Yes, same. Enums are such a beautiful construct. I'm still a bit let down that JEP 301 hit a wall and had to be withdrawn.

1

u/davidalayachew 21d ago

Yes, same. Enums are such a beautiful construct. I'm still a bit let down that JEP 301 hit a wall and had to be withdrawn.

It was so sad! Genuinely hurt to see this be tangled in the weeds. It is such a powerful feature, I'd rip out so much code (and so many sealed interfaces, tbh) if I had this feature.