Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JEP 455: Primitive Types in Patterns, instanceo...

ihcomega56
September 12, 2024

JEP 455: Primitive Types in Patterns, instanceof, and switch (Preview)

JJUG ナイトセミナー 2024年9月

ihcomega56

September 12, 2024
Tweet

More Decks by ihcomega56

Other Decks in Technology

Transcript

  1. Θͨ͠ • Α͜ͳ☕ !JIDPNFHB  • ʰ͍ͪ͹Μ΍͍͞͠(JU(JU)VCͷڭՊॻʱ • ΞϝϦΧ͔Βؼࠃ͠೥ʂ •

    ࢲ΋΂ΖΜ΂ΖΜʹͳΔͷ͕޷͖ ʢ++6(װࣄͷօ͞Μ͢Έ·ͤΜʣ 2
  2. γϯϓϧͳྫ String season = "Ն"; String result = switch (season)

    { ɹɹcase "य़" -> "Spring"; ɹɹcase "Ն" -> "Summer"; ɹɹcase "ळ" -> "Fall"; ɹɹcase "ౙ" -> "Winter"; ɹɹdefault -> "something wrong"; } 10 定数パターン というやつ
  3. ࢖͏ྫͷ঺հʢৄࡉ͸ޙ΄Ͳʣ 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 {}
  4. 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()); } キャスト不要で 簡潔かつ安全
  5. 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"; }
  6. 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(); }; オブジェクト型で パターンマッチ、 かなり簡潔! 判定と使⽤を 分けずに書ける タイプパターン というやつ パターン変数 スコープはマッチしたケースの中のみ
  7. 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を使って 構造を活⽤ 値の分解も可能
  8. 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を省略できる
  9. ؔ࿈ػೳͷ੝Γ߹Θͤ 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の判定も できる
  10. ύλʔϯϚονؔ࿈ػೳͷมભ ϓϨϏϡʔ ਖ਼ࣜϦϦʔε 19 TXJUDIࣜ JOTUBODFPG ύλʔϯϚον Ϩίʔυ γʔϧΫϥε TXJUDI

    ύλʔϯϚον Ϩίʔυ ύλʔϯ ແ໊ม਺ ແ໊ύλʔϯ ϓϦϛςΟϒܕ ύλʔϯ                                   
  11. ϓϦϛςΟϒܕͷύλʔϯϚον 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 + "೥͸Ӟ೥Ͱ͢"; };
  12. কདྷɿؔ࿈͢Δ$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の イミュータブルな 特性をいかせる