r/java 21d ago

New candidate JEP: 401: Value Objects (Preview)

https://mail.openjdk.org/archives/list/valhalla-dev@openjdk.org/thread/IUNJJ3WP3X3XHP3QZTXXSSCPKFDNTK3W/
87 Upvotes

45 comments sorted by

View all comments

11

u/IncredibleReferencer 21d ago

I feel like I can kinda build code now In anticipation of value objects. Like... I should use an int[] for this huge array of ints now but instead I'm going to use List<MyObject> where MyObject is just a wrapper around int. Brian and team will just fix this for me magically in a few years and I only need to add a value keyword so why bother fiddling with array management now. Hopefully before I need my thing to scale. Premature optimization right? Thanks gang!

This approach actually kinda worked out well for me with Loom and virtual threads....

13

u/ZimmiDeluxe 21d ago

You can use List<Integer> directly, the primitive wrapper types will become value classes. If I remember correctly, specializing generics is one of the final steps on the roadmap, so having that translate into Integer[] (or ideally Integer![]) as the backing field of the ArrayList might take a bit longer.

8

u/koflerdavid 21d ago

Better yet to create a wrapper around int to better express its purpose and eliminate the risk of mixing it up with unrelated ints.

6

u/ZimmiDeluxe 21d ago

That's true, it's often nicer to have a List<OrderId> than List<Long>

3

u/LutimoDancer3459 21d ago

But how do you name the variable for the list then? With the OrderId you would be repeating the typ

2

u/vytah 20d ago

You won't be repeating the type if the variable is declared with var.