Finalizing instanceof Pattern Matching and Records in JDK 16

原文はこちら。
The original entry was written by Dustin Marx.
http://marxsoftware.blogspot.com/2020/08/instanceof-pattern-matching-records.html

Gavin Biermanがamber-spec-expertsメーリングリストにinstanceofパターンマッチングとRecordに関する2つのわかりやすい投稿をしました。いずれも網羅的ではありますが簡潔にまとめられています。これら2つのProject Amberイニシアティブの最新状況に興味がある人は、これらの投稿を読むことをお勧めします。

Gavin Bierman
https://www.linkedin.com/in/gavin-bierman-a0173075/
Pattern Matching for instanceof
https://docs.oracle.com/en/java/javase/15/language/pattern-matching-instanceof-operator.html
Record Classes
https://docs.oracle.com/en/java/javase/15/language/records.html
amber-spec-expertsメーリングリスト
https://mail.openjdk.java.net/mailman/listinfo/amber-spec-experts
Project Amber
https://openjdk.java.net/projects/amber/
Finalizing in JDK 16 – Pattern matching for instanceof
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002253.html
Finalizing in JDK 16 – Records
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002254.html
JDK 14/JEP 305 instanceof Pattern Matching “Smart Casts”
http://marxsoftware.blogspot.com/2020/02/jdk14-instanceof-pattern-matching.html
Records Come to Java
https://blogs.oracle.com/javamagazine/records-come-to-java
JDK 16
https://openjdk.java.net/projects/jdk/16/

Pattern Matching instanceof Operator

“Finalizing in JDK 16 – Pattern matching for instanceof” で、Biermanは以下のように書いています。

“In JDK 16 we are planning to finalize two JEPs: Pattern matching for instanceof and Records.” (JDK 16で2個のJEP、Pattern matching for instanceofRecordsを最終化する予定です)

Finalizing in JDK 16 – Pattern matching for instanceof
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002253.html

instanceofパターンマッチングについて、Biermanは以下のように述べています。

“Adding conditional pattern matching to an expression form is the main technical novelty of our design of this feature.”(条件付きパターンマッチングを表現形式に追加することが、この機能の設計の主な技術的な目新しさです)

Finalizing in JDK 16 – Pattern matching for instanceof
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002253.html

“Finalizing in JDK 16 – Pattern matching for instanceof” では、instanceof パターンマッチングアプローチの利点を述べています。この投稿で議論されている利点には、「一般的なプログラミングパターン」(instanceofをチェックして、instanceofでチェックされたものを明示的に照らし合わせた型にキャストする)をリファクタリングして、より読みやすい小さなコードにすることが含まれています。もう一つは、「文を使って不必要に複雑になっているものを式でコンパクトに表現できる 」という点です。

Biermanは、スコープの問題、ローカルパターン変数のpoisoning、そして flow scopingを使用してこれらをどのように処理するかについても議論しています。Biermanは次のように書いています。

Java already uses flow analysis – both in checking the access of local variables and blank final fields, and detecting unreachable statements. We lean on this concept to introduce the new notion of flow scoping. A pattern variable is only in scope where the compiler can deduce that the pattern has matched and the variable will be bound. This analysis is flow sensitive and works in a similar way to the existing analyses. … The motto is “a pattern variable is in scope where it has definitely matched”. This is intuitive, allows for the safe reuse of pattern variables, and Java developers are already used to flow sensitive analyses.(Javaはすでにフロー解析 (flow analysis) を使って、ローカル変数や空白の最終フィールドのアクセスをチェックしたり、到達不可能な文を検出したりしています。私たちはこのコンセプトに学び、フロー・スコーピングという新しい概念を導入します。パターン変数は、コンパイラがパターンにマッチし、変数がバインドされると推論できるスコープ内にのみ存在します。この分析はフローに敏感であり、既存の分析と同様の方法で動作します。… モットーは、「パターン変数が確実にマッチしたスコープ内にある」ということです。これは直感的であり、パターン変数の安全な再利用を可能にします。Java開発者はすでにフローセンシティブな分析に慣れています。)

Finalizing in JDK 16 – Pattern matching for instanceof
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002253.html

instanceof パターンマッチングの投稿は「スイスチーズの性質」の議論で終え、IDEが開発者がこれらの不幸な相互作用に対応するのを助けることへの期待を表明しています。

“We call this unfortunate interaction of flow scoping and shadowing the “Swiss cheese property”. To rule it out would require ad-hoc special cases or more features, and our sense is that will not be that common, so we have decided to keep the feature simple. We hope that IDEs will quickly come to help programmers who have difficulty with flow scoping and shadowing.”(フロースコーピングとシャドーイングの不幸な相互作用を「スイスチーズの性質」と呼びますが、これを除外するには、アドホックな特殊なケースもしくはより多くの機能を求められるでしょう。フロースコーピングとシャドーイングに悩んでいる開発者を支援するために速やかにIDEが登場することを願っています)

Finalizing in JDK 16 – Pattern matching for instanceof
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002253.html

Records

Biermanの “Finalizing in JDK 16 – Records” の投稿では、Recordに関連したいくつかの素晴らしい要約説明が掲載されています。まず、

“Record classes are a special kind of class that are used primarily to define a simple aggregate of values.”(Recordクラスは特殊なクラスであり、主としてシンプルな値の集合を定義するために利用します)

Finalizing in JDK 16 – Records
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002254.html

この直後に以下の文章が続きます。

“Records can be thought of as nominal tuples; their declaration commits to a description of their state and given that their representation, as well as all of the interesting protocols an object might expose — construction, property access, equality, etc — are derived from that state description.”(Recordは名目上のタプル(tuple)と考えることができるため、Recordの宣言はRecordの状態表現にコミットします。Recordの宣言は、Recordの状態の記述を約束します。そしてその表現を考えると、オブジェクトが公開する可能性のあるコンストラクション、プロパティアクセス、同等性といったすべての興味深いプロトコルが、同様にその状態記述から派生しています)

Finalizing in JDK 16 – Records
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002254.html

この投稿では、Recordの状態記述(state description)からRecordのメンバーの数字を暗黙的に宣言する(implicit declaration of a number of members)も取り扱っています。この投稿では、開発者が明示的に標準的なコンストラクタを提供する理由、そしてその方法についても簡単に説明しています。これについては、Biermanが以下のように記述しています。

“The intention of a compact constructor declaration is that only validation and/or normalization code need be given in the constructor body; the remaining initialization code is automatically supplied by the compiler.”(コンパクトなコンストラクタ宣言の意図は、検証コードや正規化コードのみをコンストラクタ本体に記述する必要があり、残りの初期化コードは自動的にコンパイラが供給します)

Finalizing in JDK 16 – Records
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002254.html

“Finalizing in JDK 16 – Records”では、標準コンストラクタのアクセシビリティ、@Override、および Recordに関連する一般的なアノテーションについても言及しています。最後に、この投稿ではRecordはローカルで宣言できることに言及し、これがRecordの一般的なユースケースでどのような利点があるかを説明しています。

“Records will often be used as containers for intermediate data within method bodies. Being able to declare these record classes locally is essential to stop proliferation of classes.”(Recordをメソッド本体内の中間データのコンテナとして使用することが多々あるでしょう。これらのRecordクラスをローカルで宣言できることは、クラスの増殖を止めるために不可欠です)

Finalizing in JDK 16 – Records
https://mail.openjdk.java.net/pipermail/amber-spec-experts/2020-July/002254.html

Conclusion

このinstanceofパターンマッチングとRecordsの、2個のProject Amberイニシアティブは、歓迎すべきJavaプログラミング言語への追加機能となるでしょう。これらの機能は、JDK 15までのJDKのバージョンではプレビュー機能として提供されていましたが、より多くの開発者が本番のコードに適用できるよう、(JDK 16で)最終化されます。

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中