Share
Embed
JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview)
JJUG ナイトセミナー 2024年9月
ihcomega56
September 12, 2024
Transcript
26 slides
-
JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview)
2024/09/12 JJUGナイトセミナー -
Θͨ͠ • Α͜ͳ☕ !JIDPNFHB • ʰ͍ͪΜ͍͞͠(JU(JU)VCͷڭՊॻʱ • ΞϝϦΧ͔Βؼࠃ͠ʂ •
ࢲΖΜΖΜʹͳΔͷ͕͖ ʢ++6(װࣄͷօ͞Μ͢Έ·ͤΜʣ 2 -
γϯϓϧͳྫ String season = "Ն"; String result = switch (season)
{ ɹɹcase "य़" -> "Spring"; ɹɹcase "Ն" -> "Summer"; ɹɹcase "ळ" -> "Fall"; ɹɹcase "ౙ" -> "Winter"; ɹɹdefault -> "something wrong"; } 10 定数パターン というやつ -
͏ྫͷհʢৄࡉޙ΄Ͳʣ 12 sealed interface UserAction permits Click, Drag, Scroll {}
record Click(int x, int y) implements UserAction {} record Drag(int startX, int startY, int endX, int endY) implements UserAction {} record Scroll(int amount) implements UserAction {} -
JOTUBODFPGͰύλʔϯϚον 13 if (action instanceof Click) { Click click =
(Click) action; System.out.println("Clicked " + click.getX() + ", " + click.getY()); } if (action instanceof Click click) { System.out.println("Clicked " + click.getX() + ", " + click.getY()); } キャスト不要で 簡潔かつ安全 -
TXJUDIͰύλʔϯϚον 14 if (action instanceof Click) { Click c =
(Click) action; result = "Clicked " + c.getX() + ", " + c.getY(); } else if (action instanceof Drag) { Drag d = (Drag) action; result = "Dragged " + d.startX() + " to " + d.endX(); } else if (action instanceof Scroll) { Scroll s = (Scroll) action; result = "Scrolled " + s.getAmount(); } else { result = "Unknown action"; } -
TXJUDIͰύλʔϯϚον 15 String result = switch (action) { case Click
c -> "Clicked " + c.x() + ", " + c.y(); case Drag d -> "Dragged " + d.startX() + " to " + d.endX(); case Scroll s -> "Scrolled " + s.amount(); }; オブジェクト型で パターンマッチ、 かなり簡潔! 判定と使⽤を 分けずに書ける タイプパターン というやつ パターン変数 スコープはマッチしたケースの中のみ -
TXJUDIͰύλʔϯϚον 16 String result = switch (action) { case Click
c -> "Clicked " + c.x() + ", " + c.y(); case Drag d -> "Dragged " + d.startX() + " to " + d.endX(); case Scroll s -> "Scrolled " + s.amount(); }; String result = switch (action) { case Click(int x, int y) -> "Clicked " + x + ", " + y; case Drag(int startX, int startY, int endX, int endY) -> "Dragged " + startX + " to " + endX; case Scroll(int amount) -> "Scrolled " + amount; }; Recordを使って 構造を活⽤ 値の分解も可能 -
3FDPSEɺγʔϧ 17 sealed interface UserAction permits Click, Drag, Scroll {}
record Click(int x, int y) implements UserAction {} record Drag(int startX, int startY, int endX, int endY) implements UserAction {} record Scroll(int amount) implements UserAction {} Recordでクラス定義の ボイラープレートを減らせる (getter, equals, hashCode, toString) パターンの網羅性を担保、 defaultを省略できる -
ؔ࿈ػೳͷΓ߹Θͤ 18 String result = switch (action) { case null
-> "No action provided"; case Click(int x, int y) when x > 0 && y > 0 -> "Clicked at positive coordinates"; case Click(int x, int y) -> "Clicked at " + x + ", " + y; case Drag(int startX, int startY, int endX, int endY) -> "Dragged from " + startX + " to " + endX; case Scroll(int amount) when amount > 0 -> "Scrolled down by " + amount; case Scroll(int amount) when amount < 0 -> "Scrolled up by " + amount; case Scroll _ -> "No scrolling"; }; ガードパターンで 条件を追加できる 無名パターンで 変数が不要な時は 省略できる モデルの構造を変えずに 機能追加がしやすい nullの判定も できる -
ύλʔϯϚονؔ࿈ػೳͷมભ ϓϨϏϡʔ ਖ਼ࣜϦϦʔε 19 TXJUDIࣜ JOTUBODFPG ύλʔϯϚον Ϩίʔυ γʔϧΫϥε TXJUDI
ύλʔϯϚον Ϩίʔυ ύλʔϯ ແ໊ม ແ໊ύλʔϯ ϓϦϛςΟϒܕ ύλʔϯ -
ϓϦϛςΟϒܕͷύλʔϯϚον 22 String result = switch (value) { case int
i when i < 0 -> "Negative number: " + i; case 0 -> "Zero"; default -> "Positive number: " + value; }; return switch (year) { case int y when (y % 400 == 0) -> y + "ӞͰ͢"; case int y when (y % 100 == 0) -> y + "ӞͰ͋Γ·ͤΜ"; case int y when (y % 4 == 0) -> y + "ӞͰ͢"; }; -
কདྷɿؔ࿈͢Δ$BOEJEBUF 24 record Point(int x, int y) { } Point
oldLoc = new Point(1, 2); // x࠲ඪΛมߋͯ͠৽͍͠ϨίʔυΛ࡞ Point newLoc = oldLoc with { x = 0; }; // xͱy࠲ඪΛ2ഒʹͯ͠৽͍͠ϨίʔυΛ࡞ Point finalLoc = newLoc with { x *= 2; y *= 2; }; シンプルに書けるし Recordの イミュータブルな 特性をいかせる -
͓͞Β͍ • ύλʔϯϚονΛ׆༻ͯ͠ϘΠϥʔϓϨʔτ͕ݮΒͤՄಡੑ͕ ্͕Δ • ݱঢ়+BWBͷύλʔϯϚονͰJOTUBODFPGTXJUDIΛ׆༻͢Δ • ܕΛͬͯ҆શͳίʔυΛॻ͖͘͢ͳΔ • Ωϟετͷϛε͕ݮΒͤͨΓ
• γʔϧΫϥεͰ࿙Εෆ͕ग़དྷͨΓ • ܕΛ׆༻͢Δ্ͰϨίʔυ͔ܽͤͳ͍ 25