Live data from Hacker News

Java: Rethink Domain Primitives with Valhalla

dfa1.github.io

11–13 of 13 posts

Re: Java: Rethink Domain Primitives with Valhalla

#11
post #5
post #2

Valhalla removes the historical performance tax on tiny domain objects. This article argues that value classes finally make domain‑safe primitives practical in real systems — strong invariants, flat layout, no wrapper overhead. I’d love feedback from folks who’ve experimented with Valhalla or have opinions on modelling domain types in Java.

> Value classes cannot be null. Won't this be an issue for JPA/JSONB? `null` is a an important and value in JSON and nearly every database, and for modeling the real world.

True: this compiles and the test passes today on JDK 27-jep401ea3:

  record Person(String name, Age age, Iban iban) {}

  // {"name": "SuperMario", "age": null, "iban": null}
  Person result = mapper.readValue(json, Person.class);

  assertThat(result.age()).isNull();   // passes — value class field, null
  assertThat(result.iban()).isNull();  // passes — value class field, null

  Jackson bypasses the constructor for null JSON tokens (getNullValue() short-circuits), so the
  validation in new Age(...) never runs. Both Age and Iban are value classes; both fields are null.
What do you think? Of course, there are limitations as Person cannot be flattened anymore but I guess Valhalla will ship more JEPs later?

Re: Java: Rethink Domain Primitives with Valhalla

#12
post #4

Oh cool. It's newtype from Haskell 1.3 (1996). https://www.haskell.org/definition/from12to13.html#newtype

No idea why dfa11's sibling comment got nuked. Looks correct to me.

I guess you can probably do the same with Unpacking.

https://wiki.haskell.org/Performance/Data_types

Re: Java: Rethink Domain Primitives with Valhalla

#13
post #3
post #2

Valhalla removes the historical performance tax on tiny domain objects. This article argues that value classes finally make domain‑safe primitives practical in real systems — strong invariants, flat layout, no wrapper overhead. I’d love feedback from folks who’ve experimented with Valhalla or have opinions on modelling domain types in Java.

I've been working in Java for more than 20 years now, the last 10 with unperformant, yet performance critical enterprise slop. I can only really say one thing here: they are really late to the party with this but it's good. The performance boost will be welcome, and would probably change ORMs forever if applied right. Nullability rules should of course be also part of this domain-constricted types concept, since it w…

yeah 100% agree! In some critical area, I'm forced to drop all the "good things" and pack 2 shorts into 1 int to build a tuple(short,short)...
Post reply on HN