File: Expr.java
sealed interface Expr
permits Expr.Add, Expr.Sub, Expr.Mul, Expr.Div, Expr.Const {
record Add(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() + right.eval();
}
}
record Sub(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() - right.eval();
}
}
record Mul(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() * right.eval();
}
}
record Div(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() / right.eval();
}
}
record Const(double value) implements Expr {
public double eval() {
return value;
}
}
double eval();
default String str() {
return switch(this) {
case Add(Expr l, Expr r) ->
String.format("(%s + %s)", l.str(), r.str());
case Sub(Expr l, Expr r) ->
String.format("(%s - %s)", l.str(), r.str());
case Mul(Expr l, Expr r) ->
String.format("(%s * %s)", l.str(), r.str());
case Div(Expr l, Expr r) ->
String.format("(%s / %s)", l.str(), r.str());
case Const(double value) ->
String.format("(%f)", value);
};
}
public static void main(String[] a) {
var expr = new Add(
new Mul(new Const(23.44), new Const(Math.PI)),
new Div(new Const(3.9), new Const(11.33)));
System.out.printf("%s = %f\n", expr.str(), expr.eval());
}
}
$ ./javac --enable-preview --source 19 Expr.java
Note: Expr.java uses preview features of Java SE 19.
Note: Recompile with -Xlint:preview for details.
$ ./java --enable-preview Expr
Error: LinkageError occurred while loading main class Expr
java.lang.ClassFormatError: Method $proxy$left in class Expr has illegal modifiers: 0x1008
sealed interface Expr
permits Expr.Add, Expr.Sub, Expr.Mul, Expr.Div, Expr.Const {
record Add(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() + right.eval();
}
}
record Sub(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() - right.eval();
}
}
record Mul(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() * right.eval();
}
}
record Div(Expr left, Expr right) implements Expr {
public double eval() {
return left.eval() / right.eval();
}
}
record Const(double value) implements Expr {
public double eval() {
return value;
}
}
double eval();
default String str() {
return switch(this) {
case Add(Expr l, Expr r) ->
String.format("(%s + %s)", l.str(), r.str());
case Sub(Expr l, Expr r) ->
String.format("(%s - %s)", l.str(), r.str());
case Mul(Expr l, Expr r) ->
String.format("(%s * %s)", l.str(), r.str());
case Div(Expr l, Expr r) ->
String.format("(%s / %s)", l.str(), r.str());
case Const(double value) ->
String.format("(%f)", value);
};
}
public static void main(String[] a) {
var expr = new Add(
new Mul(new Const(23.44), new Const(Math.PI)),
new Div(new Const(3.9), new Const(11.33)));
System.out.printf("%s = %f\n", expr.str(), expr.eval());
}
}
$ ./javac --enable-preview --source 19 Expr.java
Note: Expr.java uses preview features of Java SE 19.
Note: Recompile with -Xlint:preview for details.
$ ./java --enable-preview Expr
Error: LinkageError occurred while loading main class Expr
java.lang.ClassFormatError: Method $proxy$left in class Expr has illegal modifiers: 0x1008
- duplicates
-
JDK-8288119 Method $proxy$xxx has illegal modifiers: 0x1008
- Closed
-
JDK-8287888 Deconstruction pattern matching causing runtime exception
- Closed