anonymous records as an implementation for tuples in Java
david Grajales
david.1993grajales at gmail.com
Wed Dec 11 15:58:40 UTC 2024
I've noticed that the topic of tuples in Java has come up recently, and I
wanted to take this opportunity to show an idea regarding the use of
*"anonymous"
records* as a potential implementation for tuples.
The idea would be to create *ad-hoc records* on the fly for short lived
methods, which don’t have a formal name but are defined by their
components. For example, imagine something like this:
var tuple = (int id: 10, String name: "name");
This would allow us to create simple, unnamed records with specified fields
for quick, on-the-fly usage. Accessing values from the tuple could then
work like this:
var myTupleId = tuple.id()
for passing them as arguments to methods it could be something like this.
void foo(Tuple<Integer, String> tuple){}
The idea is that, as records are just classes with special constraints to
force invariants, tuples could be records with special constraints, for
example as they would be meant to be created on the fly for pin point
needs, they should not have validation in the constructor or overriding of
getters, but they would still get automatic equals(), hashCode(), and
toString() methods.
I don't know how problematic or bad this approach would be if there were
plans to ever introduce construct tuples to Java.
best regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20241211/e50f3f47/attachment.htm>
More information about the amber-dev
mailing list