<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="line-height: normal; margin: 0px; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
In this case, it is rather straight-forward to experiment—given Gatherers—and be able to make progress on ergonomics without waiting for the standard library to offer a built-in.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="Signature" style="color: inherit;">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
√</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Nir Lisker <nlisker@gmail.com><br>
<b>Sent:</b> Monday, 28 April 2025 13:21<br>
<b>To:</b> Viktor Klang <viktor.klang@oracle.com><br>
<b>Cc:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> [External] : Re: Casting gatherer</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi Viktor,
<div><br>
</div>
<div>stream.filter(myClass:isInstance).map(myClass:cast)</div>
<div><br>
</div>
<div>is indeed the current way of doing it. I've written this code many times and I suspect others have too. An extracted version might reside in many utility classes. This is why I suggested having a Gatherer for it in Gatherers. I've written a naive implementation:</div>
<br>
private static <T, R> Gatherer<T, ?, R> filterCast(Class<R> type) {<br>
    return Gatherer.of(<br>
        (_, element, downstream) -> {<br>
            if (type.isInstance(element)) {<br>
                return downstream.push(type.cast(element));<br>
            }<br>
            return true;<br>
        }<br>
    );<br>
}
<div><br>
</div>
<div>which we might start seeing in gatherer libraries.</div>
<div><br>
</div>
<div>We don't need to be able to write 'x instanceof T', although it would be useful in many cases. It's a discussion for Valhalla. Valhalla will also allow us to use this for "non-class based" elements when generics over primitives arrive, so this gatherer
 is no different than the others.</div>
<div><br>
</div>
<div>As noted above, Amber might offer solutions too. Obviously, lower-level solutions from Valhalla and Amber are preferable, but I don't know what the plans are. Maybe you can discuss this internally. Otherwise, do you think this is a good fit?</div>
<div><br>
</div>
<div>-- Nir</div>
</div>
<br>
<div class="x_gmail_quote x_gmail_quote_container">
<div dir="ltr" class="x_gmail_attr">On Mon, Apr 28, 2025 at 12:02 PM Viktor Klang <<a href="mailto:viktor.klang@oracle.com">viktor.klang@oracle.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div class="x_msg-853807503473416099">
<div dir="ltr">
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
The challenge here is that there is no, current, reification of a pattern application, so all it boils down to at this point is: given a Predicate for some type T, make a cast to some unrelated type R.</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="line-height:normal; margin:0px; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
For Class-based retention of elements, it is possible to do the equivalent of testing Class::isInstance(element) and then push the element downstream after a Class::cast(element)</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div id="x_m_2826572262521456185Signature" style="color:inherit">
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Cheers,<br>
√</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<b><br>
</b></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<b>Viktor Klang</b></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Software Architect, Java Platform Group<br>
Oracle</div>
</div>
<div id="x_m_2826572262521456185appendonsend"></div>
<hr style="display:inline-block; width:98%">
<div id="x_m_2826572262521456185divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> core-libs-dev <<a href="mailto:core-libs-dev-retn@openjdk.org" target="_blank">core-libs-dev-retn@openjdk.org</a>>
 on behalf of Nir Lisker <<a href="mailto:nlisker@gmail.com" target="_blank">nlisker@gmail.com</a>><br>
<b>Sent:</b> Saturday, 26 April 2025 20:55<br>
<b>To:</b> <a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a> <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Subject:</b> Casting gatherer</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi,
<div><br>
</div>
<div>instanceof has been refitted to include an auto-cast ("pattern matching on instanceof"). Unfortunately, doing this as an intermediate operation on a stream requires first to filter via instanceof and then map via a cast. This is because</div>
<div><br>
</div>
<div>    x instanceof MyClass myClass</div>
<div><br>
</div>
<div>returns a boolean, not myClass.</div>
<div><br>
</div>
<div>I've asked for an easier way of doing it long ago directly on Stream and was declined, but now with Gatherers I'm bringing this up again. I think it would be reasonable to put such an operation in the Gatherers class. I imagine that many Gatherer libraries,
 or utility classes, will include it since it's a common operation, and having it in the JDK means that it'll be done the best way possible (you can optimize where others can't, if applicable here).</div>
<div><br>
</div>
<div>-- Nir</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</body>
</html>