<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4" face="monospace">Yes, this is one of those ideas that
sounds good for the first few minutes. (C# has explored something
similar with what they call "anonymous classes", which turned out
to be mostly disappointing.)<br>
<br>
The problem is _linkage_. It's easy to write out the creation
expression:<br>
<br>
var tuple = (int id: 10, String name: "foo")<br>
<br>
So now: what's the type of `tuple`? What can I assign it to? How
do I extract the members? How do I write a method that accepts a
tuple of (int id, String name)? What if someone passes it a tuple
of (int x, String s)? What does equals and hashCode do on these
things? These things have many potential answers, but none of
them are very satisfying.<br>
<br>
If you're going to create a tuple, presumably someone wants to
consume it, store it somewhere, extract its components. C#'s
answer was that you can only do so _within a compilation unit_
(effectively, within the same class) because there was no
guarantee that one classes { x: 1, y: 2 } was interoperable with
another. <br>
<br>
Linkage in Java is nominal; when you call a method `m("foo")`, the
caller and declaration have to agree on the types, and the method
descriptor (in this case, `(Ljava/lang;String;)V`) is recorded in
the classfile. if you want to be able to pass a tuple to a
method, we have to record something, and that means either
inventing a whole new structural type system to ensure that tuples
of the wrong type don't get exchanged, or erasing everything to
some "Tuple" class (in which case you lose names and likely types
of components.) <br>
<br>
DISCLAIMER: this is not meant to be a canonical explanation of why
we can't have it. It is literally the first 30 seconds of stuff
off the top of my head about why this is more complicated, from
the perspective of how the language holds together, than it looks.<br>
<br>
Essentially, this doesn't really give you the expressive power
people want from tuples; what it gives you is a convenient
syntactic shorthand for packing the elements into a blob. Which
makes it a fairly weak feature, and one where, once we had it,
people would immediately see its weaknesses and then want more.
So it is not any kind of shortcut; it is mostly convincing
ourselves that concise creation syntax is the only important thing
we get from tuples. But I don't think that's a good idea.<br>
<br>
I would put this problem in the same bucket as "collection
literals" -- optimized syntactic forms for common structural
shapes such as lists. This is on our radar but there are a number
of higher-priority things ahead of it, so we won't be talking
about it for a while.<br>
<br>
<br>
</font><br>
<br>
<div class="moz-cite-prefix">On 12/11/2024 10:58 AM, david Grajales
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CADFQZ76F_b690kkNfmE1qO-GDohGLg3+KjJ-SodGjLK0biXucg@mail.gmail.com">
<div dir="ltr">
<p style="color:rgb(0,0,0)"><font face="arial, sans-serif">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<span class="gmail-Apple-converted-space"> </span><strong>"anonymous"
records</strong><span class="gmail-Apple-converted-space"> </span>as
a potential implementation for tuples.</font></p>
<p style="color:rgb(0,0,0)"><font face="arial, sans-serif">The
idea would be to create<span class="gmail-Apple-converted-space"> </span><strong>ad-hoc
records</strong><span class="gmail-Apple-converted-space"> </span>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:</font></p>
<p style="color:rgb(0,0,0)"><font face="arial, sans-serif"><span class="gmail-hljs-type">var</span><span class="gmail-Apple-converted-space"> </span><span class="gmail-hljs-variable">tuple</span><span class="gmail-Apple-converted-space"> </span><span class="gmail-hljs-operator">=</span><span class="gmail-Apple-converted-space"> </span>(<span class="gmail-hljs-type">int</span><span class="gmail-Apple-converted-space"> </span>id:<span class="gmail-Apple-converted-space"> </span><span class="gmail-hljs-number">10</span>, String name:<span class="gmail-Apple-converted-space"> </span><span class="gmail-hljs-string">"name"</span>);<br>
</font></p>
<p style="color:rgb(0,0,0)"><font face="arial, sans-serif">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:<br>
</font></p>
<p style="color:rgb(0,0,0)"><font face="arial, sans-serif">var
myTupleId = <a href="http://tuple.id" moz-do-not-send="true">tuple.id</a>()</font><br>
</p>
<p style="color:rgb(0,0,0)">for passing them as arguments to
methods it could be something like this. </p>
<p style="color:rgb(0,0,0)">void foo(Tuple<Integer,
String> tuple){} </p>
<p style="color:rgb(0,0,0)">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<span class="gmail-Apple-converted-space"> </span><code>equals()</code>,<span class="gmail-Apple-converted-space"> </span><code>hashCode()</code>,
and<span class="gmail-Apple-converted-space"> </span><code>toString()</code><span class="gmail-Apple-converted-space"> </span>methods.<font face="arial, sans-serif"><br>
</font></p>
<p style="color:rgb(0,0,0)"><br>
</p>
<p style="color:rgb(0,0,0)">I don't know how problematic or bad
this approach would be if there were plans to ever introduce
construct tuples to Java.</p>
<p style="color:rgb(0,0,0)">best regards.</p>
</div>
</blockquote>
<br>
</body>
</html>