Pattern Matching for instanceof (Preview 2)

Remi Forax forax at univ-mlv.fr
Sun Mar 8 14:39:09 UTC 2020


Hi all,
I've taken a look where in my source code I can use instanceof Point(var x, var y) instead of instanceof Point p.
I've discovered that i've a lot of occurrences where i only need some record components but not all of them,
by example, i've an AST that defines the declaration of a variable like this
  record LocalVarExpr(String name, Expr init) implements Expr { }

When i want to some analysis, i've a code like this:
  Env env = ...
  if (expr instanceof LocalVarExpr local) {
    visit(local.init, env);
  }

Using the DeconstructionPattern, i can transform the code to
  Env env = ...
  if (expr instanceof LocalVarExpr(var name, var init)) {
    visit(init, env);
  }
but it also introduce the local variable 'name' in the block 

Obviously, the way to solve that is to use '_' like this:
  Env env = ...
  if (expr instanceof LocalVarExpr(_, var init)) {
    visit(init, env);
  }

I wonder if introducing only the DeconstructionPattern as Point(int x, int y) and Point(var x, var y) without introducing '_' is a good idea.
Declaring a local variable you don't want make the code very artificial.

cheers,
Rémi

----- Mail original -----
> De: "jan lahoda" <jan.lahoda at oracle.com>
> À: "amber-dev" <amber-dev at openjdk.java.net>
> Envoyé: Jeudi 6 Février 2020 21:18:52
> Objet: Pattern Matching for instanceof (Preview 2)

> Hi,
> 
> Thanks to Gavin, Brian and Alex, there is a new draft JEP for Pattern
> Matching for instanceof (Preview 2):
> https://bugs.openjdk.java.net/browse/JDK-8235186
> 
> Any feedback on the JEP is more than welcome!
> 
> Thanks,
>      Jan


More information about the amber-spec-experts mailing list