I am having trouble understanding the purpose of Unconditional Patterns
David Alayachew
davidalayachew at gmail.com
Mon Jan 2 16:55:45 UTC 2023
Hello Brian,
> Mostly, the warning is confusing. In an
> earlier preview, unconditional patterns
> were prohibited in instanceof because we
> were concerned of an asymmetry between
> `instanceof X` and `instanceof X x`. The
> asymmetry was resolved and unconditional
> patterns were allowed there
Thank you for your response. I understand now why the warning was put there
in the first place - to prevent a potential point of confusion for users
until the creases were ironed out.
I also understand the pain points involving null and making sure that they
are handled properly in instanceof patterns and switch patterns. I do
appreciate how we can handle null neatly in both, with null just returning
false in instanceof, and case null being an option to us for switch
patterns.
> Unconditionality is the basic building
> block of exhaustiveness (and pattern
> dominance.)
>
> Note that unconditionality is a stronger
> condition than exhaustiveness, for reasons
> that have been explained in other mails.
If I understand correctly, this is sort of like how we use ::add as the
building block of ::addAll.
More specifically, we enforce pattern dominance in switch patterns by
ensuring that the pattern above is not unconditional for the pattern below.
And we can enforce exhaustiveness by making sure the final pattern in
switch patterns is the only one that is unconditional on the parameter.
Please correct me if either of those 2 is wrong.
> These should mean the same thing now.
I guess this is where my primary point of confusion is now.
It sounds like the creases have been ironed out at this point between
instanceof pattern matching and unconditionality. So why does the warning
still exist for instanceof pattern matching?
I would understand if this was for other forms of pattern matching, since
those may not have an answer to unconditionality yet. But it sounds like
you are telling me that instanceof does.
And I don't think it is related to the fact that I am doing this for a
record. I tested this on a String too and ran into the exact same problem.
final String s = "s";
if (s instanceof String output)
{
System.out.println("String match");
}
UnconditionalPatternsPreviewWarning.java:15: warning: [preview]
unconditional patterns in instanceof are a preview feature and may be
removed in a future release.
if (s instanceof String output)
^
1 warning
Thank you for your help and insight!
David Alayachew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230102/1938cd1e/attachment.htm>
More information about the amber-dev
mailing list