Switch expressions not working in Processing 4

I’m using Processing 4 which is on Java 17.0.2, but for some reason it won’t allow switch expressions? This compiles in other IDEs so I’m not sure what’s going on here, does anyone know the reason for this, if it is fixable, or if it should be submitted as an issue?

void setup() {
  int a = 0;
  int b = switch(a) {
    case 0 -> 0;
    default -> 1;
  };
  exit();
}
1 Like

Your switch expression isn’t using proper syntax.

See here: Reference / Processing.org

Try instead:

void setup() {
  int a = 0;
  switch(a) {
    case 0:
      print("0 case");
      break;
    default:
      println("default");
      break;
  }
  exit();
}

That’s a switch statement, I’m referring to switch expressions, a feature release in Java 14 (preview since Java 12): Switch Expressions

The syntax is definitely right because I can compile that switch expression in other compilers that are also on newer Java versions. It works in an online IDE on Java 17.0.1, and Processing 4 is on 17.0.2.

2 Likes

Ah. My mistake. It’s possible that Processing is unaware of - or simply doesn’t support - that syntax for switch expressions. While Processing code is very Java-like, it is not actually Java; instead, it is very tricky and compiles down to Java. This compiling process (sic) hides lots of implementation details (like, for example, making setup() and draw() run once and continuously).

You’ll have to change your Processing code to use Processing syntax, or you can switch to Java!

1 Like

Yeah, Processing uses a really outdated version of Java. It doesn’t support lambda syntax so you’ve gotta find a workaround. Try using the older syntax as a fix.