Pattern Matching for switch (Second Preview)
Tagir Valeev
amaembo at gmail.com
Thu Sep 23 07:42:02 UTC 2021
Hello!
Thank you, Jan!
As I discussed before, I'm not quite happy with nullity handling
design, though I understand the rationale to make patterns uniform
with local variable declarations and to be able to mechanically
refactor nested patterns to nested switches. Still probably explicit
nullity declaration in pattern (like "String?" or whatever else
syntax) worth another round of exploration? Or is everybody already
tired of nullability? :-)
Also, it would be great to squeeze underscore variables in. Sometimes,
they are useful, even without deconstruction. E.g.:
com.intellij.psi.util.JavaElementKind#fromElement
https://github.com/JetBrains/intellij-community/blob/master/java/java-psi-api/src/com/intellij/psi/util/JavaElementKind.java#L93
There are many branches where we care about the class type only:
if (element instanceof PsiAnnotation) {
return ANNOTATION;
}
if (element instanceof PsiRecordComponent) {
return RECORD_COMPONENT;
}
if (element instanceof PsiLocalVariable) {
return LOCAL_VARIABLE;
}
if (element instanceof PsiPatternVariable) {
return PATTERN_VARIABLE;
}
if (element instanceof PsiParameter) {
return PARAMETER;
}
... and so on
It's a great candidate for pattern-switch migration but having to
declare a variable in every branch is annoying.
We could also resurrect part of lambda leftovers JEP (
https://openjdk.java.net/jeps/302 ) and allow underscores in other
contexts:
- Unused lambda parameter
- Unused for-each parameter
- Unused catch parameter
For now, IntelliJ inspections suggest renaming some of these to
'ignored' to avoid warning but they are still referenceable and
pollute the name space (what if you want to have two ignored values in
the scope).
Supporting underscores looks like a simple feature to me, without very
big design space and hard decisions. We had some unsettled discussions
on whether we should allow them as method parameters (because method
parameters are visible not only inside the method, but also outside,
as part of JavaDoc or reflectively). For now, we can skip this and
return later to this part, if necessary. Just allow underscores for
unused local stuff.
With best regards,
Tagir Valeev.
On Wed, Sep 22, 2021 at 11:11 PM Jan Lahoda <jan.lahoda at oracle.com> wrote:
>
> Hi,
>
> There is a new draft JEP for preview 2 of Pattern Matching for switch here:
> https://bugs.openjdk.java.net/browse/JDK-8273326
>
> The exact changes that will be done under this round of preview are yet
> to be determined, but changes related to generics handling in pattern
> matching switches seem to be plausible.
>
> Feedback is welcome!
>
> Thanks,
> Jan
>
More information about the amber-dev
mailing list