[patterns] boolean equality expressions
Mark Mahieu
markmahieu at gmail.com
Fri Jan 10 22:24:21 UTC 2020
Hi.
The draft specification for pattern matching lists precisely which kinds of boolean expression can introduce a pattern variable [1]. It doesn't mention boolean equality expressions at all, yet should the 4 cases where the pattern expression is compared to a boolean constant be considered?
expr == true
expr != true
expr != false
expr == false
It’s the last of these which I find most interesting, because this form can sometimes be found in existing Java code when the programmer deliberately chose it in favour of the logical complement operator - perhaps to add emphasis to the condition.
I certainly see this choice made periodically, so wonder whether those programmers would hold the same expectations for examples such as the following - and perhaps lose confidence in their grasp of pattern matching semantics if the language rejects it:
if ((o instanceof Foo f) == false) {
...
} else {
f.whatever(); // invalid, but would be fine with !(o instanceof Foo f)
}
Probably not the most pressing of questions, but it's been quietly nagging me :)
Regards,
Mark
[1] http://cr.openjdk.java.net/~gbierman/jep305/jep305-20191021/specs/patterns-instanceof-jls.html#jls-6.3.1 <http://cr.openjdk.java.net/~gbierman/jep305/jep305-20191021/specs/patterns-instanceof-jls.html#jls-6.3.1>
More information about the amber-spec-observers
mailing list