alt.hn

5/19/2026 at 8:31:56 PM

Java: Rethink Domain Primitives with Valhalla

https://dfa1.github.io/articles/rethink-domain-primitives-with-valhalla.html

by dfa11

5/19/2026 at 8:31:56 PM

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.

by dfa11

5/20/2026 at 11:54:17 AM

> 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.

by exabrial

5/20/2026 at 4:52:11 PM

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?

by dfa11

5/20/2026 at 11:58:54 AM

It is not.

Null is the thing which it isn't. A null value is evidence that your modelling does not fit the domain.

by mrkeen

5/20/2026 at 12:07:36 PM

Unfortunately, the simple fact is that one can be handed a JSON document with non-existent keys, and one can be handed a database row with a null column. Often, the programmer may handed a schema from a third party they have no control over: opening a semantics debate is likely not a good course of action.

I guess since basic types, like an int or double, cannot be null, I understand why these cannot be null. This unfortunately limits their usefulness, but it's a carryover from the underlying properties of the basic type.

by exabrial

5/20/2026 at 2:12:31 PM

Value types will be optionally null. What java will introduce to the tooling is narrowing of nullness types. Hence Foo! <: Foo? <: Foo. This will assist in enabling safe domains or scope in code that are null-restricted with ease. Hence we can model around such a type system rule.

by joe_mwangi

5/20/2026 at 12:19:18 PM

I agree that it is unfortunate.

by mrkeen

5/20/2026 at 6:49:15 AM

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 would solve a lot of accidental mistakes. Because that's what a type system is for, right? Validation of programmer input.

by rf15

5/20/2026 at 7:01:26 PM

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)...

by dfa11