<div dir="ltr"><div dir="ltr"><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div style="overflow-wrap: break-word;">
We can put deconstruction patterns in interfaces (Map.Entry is a prime example), but not constructors.  At some point we may bring the notion of factory into
 the language more formally, in which case we could pair deconstruction patterns with either constructors or factories, and interfaces can have factories, so we might be able to get to something like what you’re asking about in the future, but currently that’s
 quite a few steps down the road.
</div></blockquote><div><br></div><div>Indeed. I saw the section on interfaces in the document [1] linked in your post mentioned earlier. But it seems like the presented factory and deconstructor come with implementations inside the interface, whereas I think my use case would need something like "has a constructor and a corresponding deconstructor that include the names ..." to be expressible in an interface. </div><div><br></div><div>Different implementations of such an interface could implement constructors and deconstructors in different ways and potentially with more parameters as required by the interface. For example in addition to the shown Person record implementing the Named interface we could have:</div><div><br></div><div>    record Ingredient(String name, int grams) implements Named<br></div><div> </div><div>and for my original use case (generic data traversal) it would be useful if I could somehow define the interface Named in such a way that both Person and Ingredient implement it automatically (because both records have a canonical constructor and corresponding deconstructor that include the variable "name".)</div><div><br></div><div>Instead of capturing the meaning of reconstruction expressions by using constructors (or factories) and deconstructors, a more specialized way to define such interfaces could be as follows:</div><div><br></div><div>    interface Named {</div><div>        __components(String name, /* potentially more like in record declarations */);<br></div><div>    }<br></div><div><br></div><div>Such an interface declaration could mean that implementations allow reconstruction expressions with assignments to the variable "name" and a corresponding getter.</div><div><br></div><div>I realize that support for defining such interfaces is far ahead and may never be available. I like to point out that such functionality fits records (maybe other classes with corresponding constructors and deconstructors too) and would be useful for generic data traversal (and potentially other use cases.) To complete the example given here, I'd like to be able to define:</div><div><br></div><div>    static <N extends Named> N withNewName(N named, String newName) {</div><div>        return named with { name = newName };<br></div><div>    }</div><div><br></div><div>and be able to use it for both Person and Ingredient. The hypothetical interface declaration above seems to cover this use case. I think I have a clear understanding of what it would mean but may be missing aspects I'm currently unaware of.<br></div><div><br></div><div>[1] <a href="https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md">https://github.com/openjdk/amber-docs/blob/master/eg-drafts/reconstruction-records-and-classes.md</a></div><div><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 style="overflow-wrap: break-word;"><div>Without some code in the interface for “how do I make a new one”, reconstruction expressions don’t really make sense.  It would not be reasonable to, say, look at the dynamic type of the receiver and try to guess how to make a new one based on
 that; a reconstruction expression should be given clear semantics based on the static types involved.  So for the time being, your example would be over the horizon of what reconstruction expressions could do — because it’s not even obvious what they _should_
 do in this situation.  </div>
<div>
<div><br>
<blockquote type="cite">
<div>On Nov 22, 2023, at 7:28 AM, Sebastian Fischer <<a href="mailto:mail@sebfisch.de" target="_blank">mail@sebfisch.de</a>> wrote:</div>
<br>
<div>
<div dir="ltr">
<div>Hello.</div>
<div><br>
</div>
<div>Regarding the new features for pattern matching in JDK 21 I have some interesting use cases that highlight how they enable new ways to structure programs:</div>
<div><br>
</div>
<div><a href="https://github.com/sebfisch/java21-demo/wiki/Pattern-Matching" target="_blank">https://github.com/sebfisch/java21-demo/wiki/Pattern-Matching</a></div>
<div><br>
</div>
<div>Is this the right place to discuss them?</div>
<div><br>
</div>
<div>The first example uses a file search program to illustrate how algebraic data types are useful in stream pipelines, especially in handling errors as values when using a functional programming style. The second example uses tree-structured data
 and shows how core ideas from functional programming, especially related to data traversal, can be integrated into Java, leveraging streams and other functional programming patterns. Interestingly, the operations for data traversal can be reused instead of
 implementing specialized variants for each new tree structure.<br>
</div>
<div><br>
</div>
<div>I would be interested in what you expect about how the examples might change with future developments of Project Amber, especially regarding reconstruction expressions [1] and their relation to interfaces.</div>
<div><br>
</div>
<div>For example, the interface</div>
<div><br>
</div>
<div>    interface Named { String name(); }</div>
<div><br>
</div>
<div>can be implemented automatically by a record</div>
<div><br>
</div>
<div>    record Person(String name) implements Named {}</div>
<div><br>
</div>
<div>because the record component matches the signature of the interface.</div>
<div><br>
</div>
<div>It would be useful to be able to write<br>
</div>
<div><br>
</div>
<div>    named with { name = "New Name" }</div>
<div><br>
</div>
<div>for all implementations of `Named` (not only `Person`) and I wonder how `Named` could in the future be adjusted accordingly such that records still implement it automatically.</div>
<div><br>
</div>
<div>Best,</div>
<div>Sebastian<br>
</div>
<div><br>
</div>
<div>[1] <a href="https://mail.openjdk.org/pipermail/amber-spec-experts/2022-June/003461.html" target="_blank">
https://mail.openjdk.org/pipermail/amber-spec-experts/2022-June/003461.html</a></div>
<div><br>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>

</blockquote></div></div>