0

I'm using visual studio code with Language Support for Java(TM) by Red Hat plugin. I've set the runtime in settings.json as

"java.configuration.runtimes": [
    {
        "name": "JavaSE-17",
        "path": "path/to/jdk19",
        "default": true
    }
]

The following standalone java file

import java.util.Objects;

public class Edge {
    public final int first;
    public final int second;

    public Edge(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public boolean equals(Object other) {
        return switch (other) {
            case Edge e -> (first == e.first && second == e.second) || (first == e.second && second == e.first);
            default -> false;
        };
    }

    @Override
    public int hashCode() {
        if (first < second)
            return Objects.hash(first, second);
        else
            return Objects.hash(second, first);
    }
}

gives following errors: Cannot switch on a value of type Object. Only convertible int values, strings or enum variables are permitted, Edge cannot be resolved to a variable, Syntax error on token "e", delete this token, e cannot be resolved to a variable.

IntelliJ didn't show any errors so I don't think the code is incorrect. I've tested that sealed keyword from java 17 works. Although pattern matching for switch is a preview feature, I've heard that for standalone files preview features are enabled by default. Also I've seen a video someone using this feature in vscode. How could I use the feature?

Ris
  • 165
  • 10
  • @user16320675 I was referencing to [this question](https://stackoverflow.com/questions/68827042/how-to-enable-preview-feature-in-visual-studio-code). I think the plugin supports up to jdk 17. JavaSE-18 doesn't work in the settings.json. – Ris Feb 02 '22 at 13:03
  • @user16320675 I read the post again and found out that the statement only applies to Java 16. It seems I have to explicitly enable as you said. – Ris Feb 02 '22 at 13:09
  • @user16320675 I am using jdk 19 early access build. – Ris Feb 02 '22 at 13:12
  • Updated the tag. The video I saw is [this](https://youtu.be/ifdwc6vyhCE). – Ris Feb 02 '22 at 13:16
  • @user16320675 After your comment I downgraded to jdk 17 and found out that [simply cleaning the Java language server workspace](https://stackoverflow.com/a/61165125) works after downgrading. Jdk 19 still getting errors. – Ris Feb 02 '22 at 14:23

0 Answers0