Can't use the pattern matching for switch feature in vscode with jdk 19

1.5k Views Asked by At

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?

0

There are 0 best solutions below