Patterns design question: Generics

Gavin Bierman gavin.bierman at
Fri Nov 3 10:46:40 UTC 2017


A related problem to the issue of null and pattern matching is dealing with patterns mentioning generic types. Currently, it is forbidden to use instanceof with a non-reifiable type. However, we suspect that Java programmers would expect the following to work:

ArrayList<Integer> al = ...
if (al matches List<Integer> li) {
Whereas perhaps it is to be expected that the following is suspect

Object o = ...
if (o matches List<Integer> li) {
    // How could we perform this test?
The type restatement distinction that we introduced in the previous email for dealing with null provides a way forward.

More formally, given an expression e matches U u where e has type T:

If T is assignment convertible to U then this is a type restatement pattern match, and is allowed regardless of the type U (even if it is non-reifiable).
If T is cast convertible to U, but not assignment convertible, then we emit a warning/error as per the cast conversion rules.
Do we have any other design options here?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the amber-spec-experts mailing list