<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<font size="4" face="monospace">This suffers from the same problem
as C# anonymous classes -- while you could theoretically get
nominal (x.s) access to the components within the class declaring
the anonymous class, you have no chance of stable linkage across
classes, and this is a huge limitation. Again, it reduces the
value down to something purely syntactic, and not much at that.<br>
<br>
(We explored enabling this by redefining the type of an anonymous
class creation expression to be sharper -- Foo$1 instead of Object
in this case -- but the use was so limited, and it merely teased
people into thinking it was more than it was.)<br>
</font><br>
<br>
<div class="moz-cite-prefix">On 12/11/2024 12:50 PM, Anatoly
Kupriyanov wrote:<br>
</div>
<blockquote type="cite" cite="mid:CAGwStpDA4wxshz84n04RAzfvAjm-7tS-bai0_PYTgNQhSjaBxw@mail.gmail.com">
<div dir="ltr">
<div>Agree with Brian, good point. Does not fit Java spirit,
especially nominal stuff. And imho makes code harder to
understand and maintain.<br>
</div>
<div><br>
</div>
<div>However, I could suggest a compromise to enhance anonymous
ad-hoc classes we have currently:</div>
<div><br>
</div>
<div><span style="font-family:monospace">var x = new Object() {</span></div>
<div><span style="font-family:monospace"> final int i = 5;</span></div>
<div><span style="font-family:monospace"> final String s =
"hi";<br>
</span></div>
<div><span style="font-family:monospace">};</span></div>
<div><span style="font-family:monospace">println(x.s);</span><br>
</div>
<div><br>
</div>
<div>If this could be slightly reduced with records, it would be
nice.</div>
<div>These anonymous types are naturally scoped inside method
bodies and could not pollute domains/etc.</div>
<div><br>
</div>
<div>But I am afraid that it does not fit the use-cases which
Red is after.<br>
</div>
<div><br>
</div>
</div>
<br>
<div class="gmail_quote gmail_quote_container">
<div dir="ltr" class="gmail_attr">On Wed, 11 Dec 2024 at 17:27,
Brian Goetz <<a href="mailto:brian.goetz@oracle.com" moz-do-not-send="true" class="moz-txt-link-freetext">brian.goetz@oracle.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div> <font face="monospace" size="4">I had thought my
reminder in the last reply would be sufficient, but again:
this is not the Java language design list, and "why don't
you just" proposals like this just distract us from doing
what we've actually prioritized.<br>
<br>
In the 30 seconds I could spare to look at your idea, it
seems to have all the same problems as the C# anonymous
class feature. <br>
<br>
As a side exercise, I invite you to ponder where
$TString-int lives, when classes in two separate domains
both use (String, int) and want to call each other, and
what could go wrong there. (But not on this list.)<br>
</font><br>
<br>
<div>On 12/11/2024 11:59 AM, Red IO wrote:<br>
</div>
<blockquote type="cite">
<div dir="auto">
<p dir="ltr">As tuples can be implemented entirely as
synthetic sugar I don't see the problem. Simply
desugar all denotations of the tuple type to a
structurally named type similar to arrays. Then create
1 record definition for each used type. (which I guess
is at some point done for arrays) <br>
Example:<br>
(String, int) foo() {<br>
return ("hi", 42);<br>
}<br>
void bar((String, int) tup) {<br>
System.out.println(tup.0 + tup.1);<br>
} </p>
<p dir="ltr">var x = foo();<br>
bar(x);</p>
<p dir="ltr"><br>
Becomes:</p>
<p dir="ltr">public record $Tjava_lang_String-int(String
e0, int e1) extends Tuple {} </p>
<p dir="ltr">$Tjava_lang_String-int foo() {<br>
return new $Tjava_lang_String-int("hi", 42);<br>
} </p>
<p dir="ltr">void bar($Tjava_lang_String-int tup) {</p>
<p dir="ltr">System.out.println(tup.e0() + tup.e1()) ;</p>
<p dir="ltr">}</p>
<p dir="ltr"><br>
</p>
<p dir="ltr">var x = foo();<br>
bar(x);<br>
<br>
</p>
<p dir="ltr"><br>
</p>
<p dir="ltr">This is completely possible with
preprocessing alone. Syntax, descriptor format and
named vs unnamed elements are details not worth the
initial discussion. </p>
<p dir="ltr">I think tuples are great and require no
fundamentally new features to become part of the
language. </p>
<p dir="ltr"><br>
</p>
<p dir="ltr">Great regards</p>
<p dir="ltr">RedIODev </p>
</div>
<br>
<div class="gmail_quote">
<div dir="ltr" class="gmail_attr">On Wed, Dec 11, 2024,
17:23 Brian Goetz <<a href="mailto:brian.goetz@oracle.com" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">brian.goetz@oracle.com</a>>
wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div> <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>On 12/11/2024 10:58 AM, david Grajales wrote:<br>
</div>
<blockquote type="cite">
<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> </span><strong>"anonymous"
records</strong><span> </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> </span><strong>ad-hoc
records</strong><span> </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>var</span><span> </span><span>tuple</span><span> </span><span>=</span><span> </span>(<span>int</span><span> </span>id:<span> </span><span>10</span>,
String name:<span> </span><span>"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="https://urldefense.com/v3/__http://tuple.id__;!!ACWV5N9M2RV99hQ!JWvS3-XRXfU_BDrdY-5L5h1ddl_PStJP3vNDQtkLvrxFIdBwPvzjEW-LMmG4r8MykbGgC-Y4e8oBmPElPpwijXdqZiO2$" rel="noreferrer" target="_blank" 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> </span><code>equals()</code>,<span> </span><code>hashCode()</code>,
and<span> </span><code>toString()</code><span> </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>
</div>
</blockquote>
</div>
</blockquote>
<br>
</div>
</blockquote>
</div>
<div><br clear="all">
</div>
<br>
<span class="gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="gmail_signature">WBR, Anatoly.</div>
</blockquote>
<br>
</body>
</html>