On Tue, 24 Jan 2023 13:40:37 GMT, Viktor Klang <d...@openjdk.org> wrote:
>> `coll instanceof ListN<?> list` should work. > > @forax @stuart-marks Yeah, that works. It's unfortunate that it's not > possible to match on the actual (generic) type, as then both sides of the || > could use type unification to avoid having to do the cast on the return value > as well. Submitted a commit to switch to the wildcard version. Avoiding the cast to `List<E>` requires two things: - we need a way to say at declaration that a parameter type E is covariant, this is not true for List but this is true for ImmutableList (so true for List2 and ListN). - we need a way to do an union between two types when doing pattern matching . This former is still in discussion as a feature of Valhalla (it's a part of Dan Smith Phd), the later is still in discussion as a feature of Amber, so maybe in the future this will be possible is the star aligned. ------------- PR: https://git.openjdk.org/jdk/pull/11847