※ 2019/11/15 時点です。
IntelliJ IDEAまったくと言っていいほど触ったことが無いので、自分用にメモ。
環境
IntelliJ IDEA 2019.2.4 (Community Edition)
手順
New ProjectでProject SDKに13
を選択する。
https://twitter.com/java/status/1193898422849269760 の http://blog.codefx.org/java/switch-expressions/ を参考にサンプルコードを書く。
import java.io.FileNotFoundException;
public class Main {
static enum MY_BOOLEAN {
TRUE, FALSE, FILE_NOT_FOUND,
}
public static void main(String[] args) throws Exception {
MY_BOOLEAN ternaryBool = MY_BOOLEAN.TRUE;
boolean result = switch (ternaryBool) {
case TRUE -> true;
case FALSE -> false;
case FILE_NOT_FOUND -> throw new RuntimeException("This is ridiculous!", new FileNotFoundException());
// as we'll see in "Exhaustiveness", `default` is not necessary
default -> throw new IllegalArgumentException("Seriously?! 🤬");
};
System.out.println(result);
}
}
実行すると下記のようなエラーになる。
Information:java: Errors occurred while compiling module 'untitled'
Information:javac 13.0.1 was used to compile java sources
Information:2019/11/15 16:13 - Build completed with 1 error and 0 warnings in 3 s 286 ms
Error:java: invalid source release 12 with --enable-preview
(preview language features are only supported for release 13)
Open Module Settings -> Project Settings -> Modules -> Language level を13 (Preview)
に変更する。
これで動く。language levelを変えてあげれば--enable-preview
は無くても動くらしい。