<div dir="ltr"><div dir="ltr"><div>[ This is in reply to <a href="https://mail.openjdk.org/pipermail/amber-spec-experts/2026-January/004306.html">https://mail.openjdk.org/pipermail/amber-spec-experts/2026-January/004306.html</a> on amber-spec-experts. ]</div><div><br></div><div>Hi Gavin,</div><div><br></div>The "Pattern Assignment" and "Constant Patterns" ideas sound good to me. The "Pattern Assignment" idea is a natural way to extend pattern matching, which is motivated by the frequent need to declare variables to access the "variable components" in an instance of some object type.</div><div dir="ltr"><br></div><div dir="ltr">My question is: Wouldn't the same motivation apply to generic type parameters as well?</div><div><br></div><div>Here's a trivial example. Suppose we have this class:</div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">    public record Property<T>(T value) {<br>        boolean has(T t) {<br>            return t == value;<br>        }<br>    }<br></span><br></div><div>and we want to write a method to check that a list of <span style="font-family:monospace">Property</span>'s are well-behaved.</div><div><br></div><div>Here's what we'd LIKE to do, which is use "Pattern Assignment" for the generic type variable:</div><div><br></div><div><span style="font-family:monospace">    public void verifyHas(List<Property<?>> properties) {<br>        for (Property<?> element : properties) {<br>            <span style="background-color:rgb(255,255,0)"><T> Property<T> property = element;</span><br>            T value = property.value();<br>            assert property.has(value);  // no cast needed here!<br>        }<br>    }<br></span><br></div><div>but here's what we HAVE to do today to avoid the unchecked cast:</div><div><br></div><div><span style="font-family:monospace">    public void verifyHas(List<Property<?>> properties) {<br>        for (Property<?> property : properties) {<br>            verifyHas(property);<br>        }   <br>    }       <br>        <br>    // Ugh. This method exists solely to declare <T> so we can avoid an unchecked cast<br>    private <T> void verifyHas(Property<T> property) {<br>        T value = property.value();<br>        assert property.has(value);<br>    }</span></div><div><br></div><div>If we're going to add "Pattern Assignment" it seems like it would be reasonable for generic type variables to also benefit.</div><div><br></div><div>Cheers,</div><div>-Archie</div><div><br></div><div><span class="gmail_signature_prefix">-- </span></div><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div></div>