Semantics of multiple patterns with the same prefix in a switch

Remi Forax forax at
Sun Jan 31 21:59:39 UTC 2021

Just to be sure, when we have a switch like

  Object object = ...
  switch(object) {
    case Box(Circle circle): ...
    case Box(Square square): ...

We have agreed that case Box(Foo ..) is equivalent to an instanceof + a call to the deconstructor,
but i don't think we have agree what the equivalent code should be,  

  if (object instanceof Box(var value)) {
    if (value instanceof Circle circle) { ... }
    else if (value instanceof Square square) { ... }

  if ((object instanceof Box(var value)) && (value instanceof Circle circle)) { ... }
  else if ((object instanceof Box(var value)) && (value instanceof Square square)) { ... }

It's important because if Box is a class with a deconstructor, i can insert a side effect in it and see how many times the deconstructor of Box is called.

Given that i don't see the point of calling the deconstructor twice, in my mind, the switch is equivalent to the code with nested ifs and not the one with plain ifs.
But re-reading the last document sent by Brian, i'm not sure anymore.


More information about the amber-spec-experts mailing list