<div dir="auto"><div>Hello! <div dir="auto"><br></div><div dir="auto">This would basically bring flow-typing to Java (variable type may be narrowed at use site, depending on the context; known as smart-casts in Kotlin). I think, this is something Java tries to avoid. That's why a instanceof B b introduces a new variable, rather than changes the type of a.</div><div dir="auto"><br></div><div dir="auto">Another problem is that container.item instanceof Number does not imply container instanceof Container<Number>. It could easily be Container<Object>. Imagine</div><div dir="auto"><br></div><div dir="auto">class Container<T> {</div><div dir="auto">  T item, item2;</div><div dir="auto">}</div><div dir="auto"><br></div><div dir="auto">If you checked that item is Number, it doesn't mean that item2 is also Number. It could be Container<Object> having Integer and String in the fields. If you pass it to a place which accepts Container<Number> and reads item2, you'll get a problem. </div><div dir="auto"><br></div><div dir="auto">It's also possible that it's Container<Integer>, not Container<Number>. Again, if you pass it to the place where Container<Number> is accepted, it can rewrite your Integer field with Double, and later the code that still assumes it's Container<Integer> will blow up.</div><div dir="auto"><br></div><div dir="auto">With best regards, </div><div dir="auto">Tagir Valeev </div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 10, 2024, 11:17 Nir Lisker <<a href="mailto:nlisker@gmail.com">nlisker@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr">Hi,<div><br></div><div>Give a simple generic wrapper,</div><div><br></div><div>class Container<T> {</div><div>    T item;</div><div>}</div><div><br></div><div>pattern matching can find out what T is for 'item', but not for its container:</div><div><br></div><div>void switchItem(Container<?> container) {</div><div>    if (container.item instanceof Number num) {</div><div>        acceptNumber(num);</div><div>        acceptNumberContainer(container); // error</div><div>    }</div><div>}</div><div><br></div><div>The information is technically there to make the derivation. I can envision some complexities with bound constraints, but seeing as I can't check 'instanceof Container<Number>`, can the compiler not be smarter?</div><div><br></div><div>- Nir</div></div>
</div>
</blockquote></div></div></div>