<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="gmail_quote gmail_quote_container"><div dir="ltr" class="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="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="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="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="m_2826572262521456185appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="m_2826572262521456185divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><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>