Overloading of matcher method Was: Deconstruction patterns

Brian Goetz brian.goetz at oracle.com
Tue Mar 7 20:51:48 UTC 2023



On 3/7/2023 3:26 PM, forax at univ-mlv.fr wrote:
> It's not exactly what i ask, the question is how to call the 
> deconstructor that takes a CharSequence with a type pattern on a String.
> The inverse of
>   String s = ...
>   new Foo((CharSequence) s)
>
> should be
>   case [Call the destructor Foo(CharSequence)] + Type Pattern (String)
>

Well, remember that you cannot really "name" a method either, you can 
only eliminate the ambiguity that would cause a different method to be 
selected.  And its the same with patterns.

If you really want to do this (and its not clear to me why this is so 
important) you can do this:

     if (target instanceof Foo(CharSequence cs) && cs instanceof String 
s) { ... }

The first clause ensures that you select the CS pattern, and the second 
refines the binding as if you had nested `String s`.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-spec-observers/attachments/20230307/dbd045c9/attachment.htm>


More information about the amber-spec-observers mailing list