Revisiting the rule for merging type patterns ?

Remi Forax forax at univ-mlv.fr
Sat Aug 28 21:42:38 UTC 2021


I believe that the rule for merging type pattern is overly restrictive,
here is an example of a method that collects all the declared variables (the language is a kind of functional JavaScript i'm using for teaching)

    private static void visitVariable(Expr expr, JSObject env) {
      switch (expr) {
        case Block block -> {
          for (Expr instr : block.instrs()) {
            visitVariable(instr, env);
          }
        }
        case Literal literal -> {
          // do nothing
        }
        case FunCall funCall -> {
          // do nothing
        }
        case LocalVarAssignment localVarAssignment -> {
          if (localVarAssignment.declaration()) {
            env.register(localVarAssignment.name(), env.length());
          }
        }
        case LocalVarAccess localVarAccess -> {
          // do nothing
        }
        case Fun fun -> {
          // do nothing
        }
        case Return _return -> {
          // do nothing
        }
        case If _if -> {
          visitVariable(_if.trueBlock(), env);
          visitVariable(_if.falseBlock(), env);
        }
        case New _new -> {
          // do nothing
        }
        case FieldAccess fieldAccess -> {
          // do nothing
        }
        case FieldAssignment fieldAssignment -> {
          // do nothing
        }
        case MethodCall methodCall -> {
          // do nothing
        }
        default -> throw new AssertionError("unknown case " + expr.getClass());
      };
    }

Here i can not group the cases that do nothing together because we have explicitly disallow it,
so the code below does not compile
  case Literal literal, FunCall funcall -> {
     // do nothing
  }

I think we should revisit that discussion and just not introduce the any bindings in that case so the example above will compile but "literal" or "funcall" are not added as local variable.

Once we will introduce '_', the "correct" way to  write such code will be
  case Literal _, FunCall _ -> {
     // do nothing
  }

regards,
Rémi


More information about the amber-spec-experts mailing list