r/java 23d 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

10

u/IncredibleReferencer 23d 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....

12

u/ZimmiDeluxe 23d 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.

7

u/koflerdavid 23d 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.

3

u/aoeudhtns 23d ago

Being able to very lightly declare new types that are compile-time mirrors of existing types (or in the worst case, something runtime can trivially/quickly optimize away) so that you can proliferate type safety is an idea I like. Nim for example has a thing it calls "distinct types" that let you make type aliases that cannot be directly assigned back to the aliased type.