Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Patterns] Mixed multiple pattern and when is not fully supported in switch expression #3109

Closed
NolwennD opened this issue Oct 17, 2024 · 3 comments · Fixed by #3110
Closed
Assignees
Milestone

Comments

@NolwennD
Copy link

NolwennD commented Oct 17, 2024

Since JEP 456 released in JDK 22 it’s possible to do this :

switch (box) {
    case Box(RedBall _), Box(BlueBall _) -> processBox(box);
    case Box(GreenBall _)                -> stopProcessing();
    case Box(var _)                      -> pickAnotherBox();
}

But Eclipse detect some errors on this valid code

package maybe;

import java.util.function.Function;
import java.util.function.Predicate;

public sealed interface Maybe<T>  {

  static <T> Maybe<T> of(T value) {
    return new Some<>(value);
  }

  static <T> Maybe<T> empty() {
    return new None<>();
  }

  default Maybe<T> filter(Predicate<T> predicate){
    return switch (this) {
      case Some(T value) when predicate.test(value) -> this; // unsupported line with 4 errors
      case Some(_), None<T> _ -> empty();
    };
  }
  
  default Maybe<T> filter2(Predicate<T> predicate){
    return switch (this) {
      case Some(_), None<T> _ -> empty(); //also unsupported with 1 error
    };
  }
  record Some<T>(T value) implements Maybe<T> {}
  record None<T>() implements Maybe<T>{}
}

Eclipse

Version: 2024-09 (4.33.0)
Build id: 20240905-0614

@srikanth-sankaran
Copy link
Contributor

I'll take a look. Thanks for the defect report.

@srikanth-sankaran srikanth-sankaran self-assigned this Oct 18, 2024
@srikanth-sankaran
Copy link
Contributor

Compiles fine on master/HEAD. Very likely a duplicate of #3003 - I'll verify, add a test case and close.

@srikanth-sankaran
Copy link
Contributor

srikanth-sankaran commented Oct 18, 2024

Yes, If I move back Scanner.java to the prior version to the fix for #3003, I get these errors:

Cannot mix pattern with other case labels
Invalid expression as statement
Syntax error, insert ":" to complete SwitchLabel
Syntax error, insert ":" to complete SwitchLabel
Syntax error on token "when", , expected

@NolwennD - are these the same errors you see ?

@srikanth-sankaran srikanth-sankaran added this to the 4.34 M1 milestone Oct 18, 2024
@srikanth-sankaran srikanth-sankaran changed the title Mixed multiple pattern and when is not fully supported in switch expression [Patterns] Mixed multiple pattern and when is not fully supported in switch expression Oct 18, 2024
srikanth-sankaran added a commit to srikanth-sankaran/eclipse.jdt.core that referenced this issue Oct 18, 2024
srikanth-sankaran added a commit that referenced this issue Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants