Skip to content

Instantly share code, notes, and snippets.

@tonivade
Last active May 30, 2020 16:27
Show Gist options
  • Save tonivade/297239e44da8192897cb7a7339f4b044 to your computer and use it in GitHub Desktop.
Save tonivade/297239e44da8192897cb7a7339f4b044 to your computer and use it in GitHub Desktop.
Java 14 bug in smart constructor
import java.util.function.Supplier;
public record Bug(int value) {
static void bug(Supplier<String> str) {
System.out.println(str.get());
}
public Bug {
bug(() -> "hello");
}
public static void main(String[] args) {
new Bug(1);
}
}
$ java --enable-preview --source 14 Bug.java
Bug.java:9: error: invalid compact constructor in record Bug(int)
public Bug {
^
(compact constructor must not have return statements)
Note: Bug.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
1 error
error: compilation failed
$ java --enable-preview --source 15 Bug.java
Bug.java:9: error: invalid compact constructor in record <init>
public Bug {
^
(compact constructor must not have return statements)
Note: Bug.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
1 error
error: compilation failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment