Record pattern and side effects

Remi Forax forax at univ-mlv.fr
Sun Apr 17 09:48:50 UTC 2022


This is something i think we have no discussed, with a record pattern, the switch has to call the record accessors, and those can do side effects,
revealing the order of the calls to the accessors.

So by example, with a code like this

  record Foo(Object o1, Object o2) {
    public Object o2() {
      throw new AssertionError();
    }
  }

  void int m(Foo foo) {
    return switch(foo) {
      case Foo(String s, Object o2) -> 1
      case Foo foo -> 2
    };
  }

  m(new Foo(3, 4));   // throw AssertionError ?

Do the call throw an AssertionError ?
I believe the answer is no, because 3 is not a String, so Foo::o2() is not called.

Rémi


More information about the amber-spec-experts mailing list