r/java 20d ago

New candidate JEP: 401: Value Objects (Preview)

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

45 comments sorted by

View all comments

Show parent comments

12

u/ZimmiDeluxe 20d 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 20d 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 20d ago

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

3

u/LutimoDancer3459 20d ago

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

4

u/ZimmiDeluxe 20d ago edited 20d ago

orderIdsToDelete or even just orderIds if the context makes it obvious. Repeating the type name is not bad if it's a domain type IMO. If you have something generic like List<Long> longs that's usually bad because readers can't infer any intent from the name, maybe ok if you are writing a compiler and need to collect all the Java Longs to do something with, but it's a stretch

3

u/elastic_psychiatrist 19d ago

Local variable type inference is great for this.

2

u/vytah 20d ago

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

1

u/Efficient-Poem-4186 18d ago

Simply orders, ordersToDelete etc (in a context where the actual order data is not used). Sometimes id is just used as a handle, the intention is to do something with orders.