Data Oriented Programming, Beyond Records

forax at univ-mlv.fr forax at univ-mlv.fr
Sun Jan 18 12:49:00 UTC 2026


> From: "Brian Goetz" <brian.goetz at oracle.com>
> To: "Remi Forax" <forax at univ-mlv.fr>, "Viktor Klang" <viktor.klang at oracle.com>
> Cc: "amber-spec-experts" <amber-spec-experts at openjdk.java.net>
> Sent: Sunday, January 18, 2026 2:00:19 AM
> Subject: Re: Data Oriented Programming, Beyond Records

> In reality, the deconstructor is not a method at all.

> When we match:

> x instanceof R(P, Q)

> we first ask `instanceof R`, and if that succeeds, we call the accessors for the
> first two components. The accessors are instance methods, but the deconstructor
> is not embodied as a method. This is true for carriers as well as for records.
Pattern matching and late binding are dual only for public types, 
if an implementation class (the one containing the fields) is not visible from outside, the way to get to fields is by using late binding. 

If the only way to do the late binding is by using accessors, then you can not guarantee the atomicity of the deconstruction, 
or said differently the pattern matching will be able to see states that does not exist. 

Let say I have a public thread safe class containing two fields, and I want see that class has a carrier class, 
with the idea that a carrier class either provide a deconstructor method or accessors. 
I can write the following code : 

public final class ThreadSafeData(String name, int age) { 
private String name; 
private int age; 
private final Object lock = new Object(); 

public ThreadSafeData(String name, int age) { 
synchronized(lock) { 
this.name = name; 
this.age = age; 
} 
} 

public void set(String name, int age) { 
synchronized(lock) { 
this.name = name; 
this.age = age; 
} 
} 

public String toString() { 
synchronized(lock) { 
return name + " " + age; 
} 
} 

public deconstructor() {. // no return type, the compiler checks that the return values have the same carrier definition 
record Tuple(String name, int age) { } 
synchronized(lock) { 
return new Tuple(name, age); 
} 
} 

// no accessors here, if you want to have access the state, use pattern matching like this 
// ThreadSafeHolder holder = ... 
// ThreadSafeHolder(String name, int age) = holder; 
} 
I understand that you are trying to drastically simplify the pattern matching model (yai !) by removing the deconstructor method but by doing that you are making thread safe classes second class citizens. 
regards, 
Rémi 

> On 1/17/2026 5:09 PM, [ mailto:forax at univ-mlv.fr | forax at univ-mlv.fr ] wrote:

>>> From: "Viktor Klang" [ mailto:viktor.klang at oracle.com |
>>> <viktor.klang at oracle.com> ]
>>> To: "Remi Forax" [ mailto:forax at univ-mlv.fr | <forax at univ-mlv.fr> ] , "Brian
>>> Goetz" [ mailto:brian.goetz at oracle.com | <brian.goetz at oracle.com> ]
>>> Cc: "amber-spec-experts" [ mailto:amber-spec-experts at openjdk.java.net |
>>> <amber-spec-experts at openjdk.java.net> ]
>>> Sent: Saturday, January 17, 2026 5:00:41 PM
>>> Subject: Re: Data Oriented Programming, Beyond Records

>>> Just a quick note regarding the following, given my experience in this area:
>>> On 2026-01-17 11:36, Remi Forax wrote:

>>>> A de-constructor becomes an instance method that must return a carrier
>>>> class/carrier interface, a type that has the information to be destructured and
>>>> the structure has to match the one defined by the type.
>> Hello Viktor,
>> thanks to bring back that point,

>>> This simply does not work as a deconstructor cannot be an instance-method just
>>> like a constructor cannot be an instance method: It strictly belongs to the
>>> type itself (not the hierarchy) and
>> It can work as you said for a concrete type, but for an abstract type, you need
>> to go from the abstract definition to the concrete one,
>> if you do not want to re-invent the wheel here, the deconstructor has to be an
>> abstract instance method.

>> For example, with a non-public named implementation

>> interface Pair<F, S>(F first, S second) {
>> public <F,S> Pair<F,S> of(F first, S second) {
>> record Impl<F, S>(F first, S second) implements Pair<F, S>{ }
>> return new Impl<>(first, second);
>> }
>> }

>> inside Pair, there is no concrete field first and second, so you need a way to
>> extract them from the implementation.

>> This can be implemented either using accessors (first() and second()) but you
>> have a problem if you want your implementation to be mutable and synchronized
>> on a lock (because the instance can be changed in between the call to first()
>> and the call to second()) or you can have one abstract method, the
>> deconstructor.

>>> it doesn't play well with implementing multiple interfaces (name clashing), and
>>> interacts poorly with overload resolution (instead of choosing most-specific,
>>> you need to select a specific point in the hierarchy to call the method).
>> It depends on the compiler translation, but if you limit yourself to one
>> destructor per class (the dual of the canonical constructor), the deconstructor
>> can be desugared to one instance method that takes nothing and return
>> java.lang.Object, so no name clash and no problem of overloading (because
>> overloading is not allowed, you have to use '_' at use site).

>>> --
>>> Cheers,
>>>
>> regards,
>> Rémi

>>> Viktor Klang
>>> Software Architect, Java Platform Group
>>> Oracle
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-experts/attachments/20260118/79c4c88c/attachment.htm>


More information about the amber-spec-experts mailing list