apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.69k stars 853 forks source link

var in Pattern Matching for Switch #7388

Open mambastudio opened 6 months ago

mambastudio commented 6 months ago

Apache NetBeans version

Apache NetBeans 21

What happened

Seems using var in pattern matching when implementing Algebraic Data Type patterns results "Exception in thread "main" java.lang.RuntimeException: Uncompilable code"

Language / Project Type / NetBeans Component

No response

How to reproduce

Seems using var in pattern matching when implementing Algebraic Data Type patterns results "Exception in thread "main" java.lang.RuntimeException: Uncompilable code"

For example, the code below throws the exception when run on netbeans

public class ADTEvaluation {

    sealed interface Expr{}

    record ConstExpr(int i) implements Expr{}
    record SumExpr(Expr left, Expr right) implements Expr{}

    static int eval(Expr e){
        return switch(e){
            case SumExpr(var a, var b) -> eval(a) + eval(b);
            case ConstExpr(int i) -> i;
            default -> 3;
        };
    }

    public static void main(String... args)
    {
        var v = new ConstExpr(3);

        var x = eval(v);
        System.out.println(x);

    }
}

But if I change the SumExpr(var a, var b), to SumExpr(ConstExpr a, ConstExpr b) in the pattern matching in switch, the following code works

public class ADTEvaluation {

    sealed interface Expr{}

    record ConstExpr(int i) implements Expr{}
    record SumExpr(Expr left, Expr right) implements Expr{}

    static int eval(Expr e){
        return switch(e){
            case SumExpr(ConstExpr a, ConstExpr b) -> eval(a) + eval(b);
            case ConstExpr(int i) -> i;
            default -> 3;
        };
    }

    public static void main(String... args)
    {
        var v = new ConstExpr(3);

        var x = eval(v);
        System.out.println(x);

    }
}

I'm running Netbeans 21, using JDK 22. There is no problem if I run the code in jdk 22 directly.

Did this work correctly in an earlier version?

No / Don't know

Operating System

Windows 10

JDK

22

Apache NetBeans packaging

Apache NetBeans provided installer

Anything else

No response

Are you willing to submit a pull request?

No

neilcsmith-net commented 6 months ago

NetBeans 21 doesn't support running on JDK 22. Try this in NB22-rc4 and see if it's still an issue.

mambastudio commented 6 months ago

Just tried the NB22-rc4, and it's still showing me the same error!

image

But if I remove var, it works well.

image

mambastudio commented 6 months ago

Here is a screenshot of my netbeans...

image

lahodaj commented 6 months ago

Thanks for the report.

I'll take a look, but as a workaround, (assuming it is the Ant project), you should be able to go to File/Project Properties to tab Build/Compiling, and unselect "Compile on Save".

mambastudio commented 6 months ago

Thanks for the report.

I'll take a look, but as a workaround, (assuming it is the Ant project), you should be able to go to File/Project Properties to tab Build/Compiling, and unselect "Compile on Save".

Thanks. Unselecting "Compile on Save" seems to work, and a good temporary solution.

neilcsmith-net commented 6 months ago

Thanks. Unselecting "Compile on Save" seems to work, and a good temporary solution.

While a fix would be welcome, it would also be good to have "Compile on Save" off with Ant by default to bring in line with other build systems.