apache / netbeans

Apache NetBeans

Home Page:https://netbeans.apache.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

var in Pattern Matching for Switch

mambastudio opened this issue · comments

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

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

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

image

But if I remove var, it works well.

image

Here is a screenshot of my netbeans...

image

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 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.

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.