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

パターンマッチングを学んで新しいJavaの世界へ!Java 18までの目玉機能をおさら...

パターンマッチングを学んで新しいJavaの世界へ!Java 18までの目玉機能をおさらいしよう / Java 18 pattern matching

ihcomega56

June 19, 2022
Tweet

More Decks by ihcomega56

Other Decks in Programming

Transcript

  1. Ξοϓσʔτͷྫ +BWB var nanikore = Map.of( 1, List.of("いぬ", "ねこ", "たぬき"),

    2, List.of("こあら", "ぱんだ")); ϩʔΧϧม਺ͷ ܕਪ࿦ 4
  2. Ξοϓσʔτͷྫ +BWB String memo = """ %s 以降 テキストブロックで 複数⾏の⽂字が

    書けるようになった """.formatted("Java 15"); ςΩετϒϩοΫ 5
  3. ύλʔϯϚονϯάͱ͸ Ұൠ࿦ 🌻 🌹 🌸 🌷 🌸 🌸 🌸 🌸

    🌷 🌹 🌻 🌸 🌻 🌹 🌸 🌷 🌻 🌹 ❓ ❓ 💐 💐 💐 💐 13
  4. ϓϨϏϡʔػೳͱ͸ • ਖ਼ࣜϦϦʔεલͷػೳ • ࣮ࡍʹ࢖ͬͯͷϑΟʔυόοΫΛಘΔͨΊެ։͞ΕΔ • KBWBD΍KBWBίϚϯυʹ FOBCMFQSFWJFXϑϥάΛ ෇͚࣮ͯߦ͢Δͱ༗ޮʹͳΔ •

    KBWBDSFMFBTFEFOBCMFQSFWJFX)PHFKBWB • KBWBFOBCMFQSFWJFX)PHF • KTIFMM΍KBWBEPDίϚϯυͰ΋࢖͑Δ • *%&Ͱ΋αϙʔτ͕͋Δ 17
  5. γϯϓϧͳύλʔϯϚονϯά String season = “夏”; String result = switch (season)

    { case “春” -> “Spring”; case “夏” -> “Summer”; case “秋” -> “Fall”; case “冬” -> “Winter”; default -> "Nothing"; } 18
  6. ϞσϧΛ༻ҙ͢Δ چ public class Text implements Memo { private Id

    id; private Title title; private Body body; public Text(Id id, Title title, Body body) { this.id = id; this.title = title; this.body = body; } // setter/getter // toString() // equals() // hashCode() } 24
  7. ϨίʔυΫϥε record Text(Id id, Title title, Body body) implements Memo

    {} • SFDPSEΩʔϫʔυ෇͖ͰΫϥεΛఆٛ͢Δ • (Id id, Title title, Body body) ෦෼ʮ Ϩίʔυ ϔομʔʯʹ ʮ Ϩίʔυ ίϯϙʔωϯτʯΛఆٛͰ͖Δ 27
  8. ϨίʔυΫϥε Լه͕ࣗಈͰੜ੒͞ΕΔ • ϔομʔͱಉ͡ϑΟʔϧυΛ࣋ͬͨίϯετϥΫλ • ֤ίϯϙʔωϯτʹରͯ͠ͷ • QSJWBUFGJOBMͳϑΟʔϧυ ίϯϙʔωϯτͱಉ໊ •

    QVCMJDͳΞΫηαʔϝιου ίϯϙʔωϯτͱಉ໊ • FRVBMT ϝιου IBTI$PEF ϝιου ಉ͡ܕͰ֤ίϯϙʔωϯτ͕ಉ͡஋Λ࣋ͬͨϨίʔυ͔Ͳ͏͔ • UP4USJOH ϝιουίϯϙʔωϯτͷ໊લͱ஋Λฦ͢ 28
  9. ϨίʔυΫϥε • ϨίʔυΫϥεɺίϯϙʔωϯτ͔Βੜ੒͞ΕͨϑΟʔϧυ͸ ҉໧తʹGJOBM ΠϯελϯεΛΠϛϡʔλϒϧʹอͭࢥ૝ • ଞͷΫϥεΛܧঝͰ͖ͳ͍ KBWBMBOH3FDPSEΛܧঝ͍ͯ͠Δ ͨΊ •

    ΠϯλʔϑΣʔεͷ࣮૷͸Ͱ͖Δ • ଞͷΫϥε͔Βͷܧঝ΋Ͱ͖ͳ͍ GJOBMͳͷͰ • Ϋϥεͱಉ༷ϝιου͕એݴͰ͖Δ • ϔομʔҎ֎ʹΠϯελϯεϑΟʔϧυ͸એݴͰ͖ͳ͍ 29
  10. ϨίʔυΫϥε public interface Memo { … record Text(Id id, Title

    title, Body body) implements Memo {} record TodoList(Id id, Title title, List<Task> tasks) implements Memo {} record Image … } • Ϋϥε΍ΠϯλʔϑΣʔεͷதʹఆٛͰ͖Δ • ೖΕࢠʹ͢Δ͜ͱͳ͘ಉϑΝΠϧʹఆٛ΋Ͱ͖Δ • ΋ͪΖΜผϑΝΠϧͰͷఆٛ΋Ͱ͖Δ ͜͏͢Ε͹ϑΝΠϧʹఆٛͰ͖Δ͕ QVCMJDͳϨίʔυʹ͍ͨ͠৔߹ ೖΕࢠʹ͢Δ͔͠ͳ͍ɻ ݟ׳Εͳ͍ίʔυʹͳΔ΋ͷͷศར 30
  11. String doSomethingForEach(Memo memo) { if (memo == null) { return

    "nullが来た"; } else if (memo instanceof Text) { Text text = (Text) memo; return doSomethingForText(text); } else if (memo instanceof TodoList) { TodoList todo = (TodoList) memo; return doSomethingForTodoList(todo); } else if (memo instanceof Image) { Image image = (Image) memo; return doSomethingForImage(image); } else { return "おかしな値が来た"; } } ϝϞͷछྨ͝ͱʹ ॲཧΛৼΓ෼͚Δ چ 32
  12. String doSomethingForEach(Memo memo) { return switch (memo) { case null

    -> “nullが来た”; case Text t -> doSomethingForText(t); case TodoList td -> doSomethingForTodoList(td); case Image i -> doSomethingForImage(i); default -> "おかしな値が来た"; }; } ϝϞͷछྨ͝ͱʹ ॲཧΛৼΓ෼͚Δ ৽ 34 ໼ҹ͕ἧ͏Α͏ͳ ϑΥʔϚοτʹ͢Δͱ ಡΈ΍͍͢
  13. case null, Text t -> “null͔ςΩετϝϞ”; case null, default ->

    “null͔͓͔͠ͳ஋”; • ࠓͷͱ͜ΖҟͳΔܕΛ·ͱΊͯॻ͘͜ͱ͸Ͱ͖ͳ͍͕ɺOVMMͱ ଞͷέʔε͸·ͱΊΒΕΔ • OVMMͷ৔߹ύλʔϯม਺Λ࢖͏ͱ౰વ/VMM1PJOUFS&YDFQUJPO ϝϞͷछྨ͝ͱʹ ॲཧΛৼΓ෼͚Δ ৽ 36
  14. String doSomethingForEach(Memo memo) { if (memo == null) { return

    “nullが来た”; } else if (memo instanceof Text) { Text text = (Text) memo; return doSomethingForText(text); } else if (memo instanceof TodoList) { TodoList todo = (TodoList) memo; if (0 < todo.tasks().size()) { return doSomethingForTodoList(todo); } else { return "ToDoが空っぽ"; } } else if (memo instanceof Image) { Image image = (Image) memo; return doSomethingForImage(image); } else { return "おかしな値が来た"; } } ৚݅Λ௥Ճ͢Δ چ 38
  15. … } else if (memo instanceof TodoList) { TodoList todo

    = (TodoList) memo; if (0 < todo.tasks().size()) { return doSomethingForTodoList(todo); } else { return "ToDoが空っぽ"; } } else if… ৚݅Λ௥Ճ͢Δ چɾ֦େ 39
  16. return switch (memo) { case null -> "nullが来た"; case Text

    t -> doSomethingForText(t); case TodoList td && 0 < td.tasks().size() -> doSomethingForTodoList(td); case TodoList td -> "ToDoが空っぽ"; case Image i -> doSomethingForImage(i); default -> "おかしな値が来た"; }; ৚݅Λ௥Ճ͢Δ ৽ 41
  17. ΨʔυύλʔϯʗύλʔϯΨʔυ case TodoList td && 0 < td.tasks().size() -> doSomethingForTodoList(td);

    case TodoList td -> "ToDoが空っぽ"; • ύλʔϯϚονϯάʹ৚݅Λ௥ՃͰ͖Δ • ৚݅ʹ߹க͠ͳ͍৔߹ͷॲཧΛॻ͖๨Εͳ͍Α͏ʹ͠Α͏ 42
  18. ϝϞͷछྨΛ૿΍͢ public interface Memo { … record Text(Id id, Title

    title, Body body ) implements Memo {} record TodoList(Id id, Title title, List<Task> tasks) implements Memo {} record Image(Id id, Title title, ImageUrl url, Body body ) implements Memo {} record Video(Id id, Title title, VideoUrl url ) implements Memo {} } 44
  19. return switch (memo) { case null -> "nullが来た"; case Text

    t -> doSomethingForText(t); case TodoList td && 0 < td.tasks().size() -> doSomethingForTodoList(td); case TodoList td -> "ToDoが空っぽ"; case Image i -> doSomethingForImage(i); case Video v -> doSomethingForVideo(v); default -> "おかしな値が来た"; }; ϝϞͷछྨΛ૿΍͢ 45
  20. ϝϞͷछྨΛ૿΍͢ sealed public interface Memo { … record Text(Id id,

    Title title, Body body ) implements Memo {} record TodoList(Id id, Title title, List<Task> tasks) implements Memo {} record Image(Id id, Title title, ImageUrl url, Body body) implements Memo {} record Video(Id id, Title title, Video Url url) implements Memo {} } 47
  21. γʔϧ υ Ϋϥε • TFBMFEΩʔϫʔυΛ͚ͭͯఆٛͨ͠Ϋϥε΍ΠϯλʔϑΣʔε • ܧঝɾ࣮૷ՄೳͳΫϥεΛԼهͷΈʹݶఆ͢Δ Ϩίʔυ΋Մ • QFSNJUT۟Λ͚ͭͯڐՄͨ͠Ϋϥε

    sealed public interface Memo permits Text, TodoList, Image, Video • ಉ͡ϑΝΠϧʹఆٛͨ͠Ϋϥε • ܕʹΑΔύλʔϯϚονϯάͰ͢΂ͯͷܕΛ໢ཏ͍ͯ͠Δ͜ͱ Λ୲อͰ͖Δ ໢ཏ͍ͯ͠ͳ͍৔߹͸ίϯύΠϧΤϥʔ • ύλʔϯϚονϯάʹ͓͚ΔEFGBVMU͕ඞਢͰͳ͘ͳΔ 48
  22. return switch (memo) { case null -> "nullが来た"; case Text

    t -> doSomethingForText(t); case TodoList td && 0 < td.tasks().size() -> doSomethingForTodoList(td); case TodoList td -> "ToDoが空っぽ"; case Image i -> doSomethingForImage(i) ; case Video v -> doSomethingForVideo(v); }; ϝϞͷछྨΛ૿΍͢ 49
  23. +BWBʹظ଴ if (memo instanceof Text(Id id, Title title, Body body))

    { show(body); } --- OR --- if (memo instanceof Text(var id, var title, var body)) { show(body); } • Ϩίʔυύλʔϯ͕ಋೖ͞ΕΔ༧ఆ ϓϨϏϡʔ • ϨίʔυΛ༻͍ͨύλʔϯϚονϯάʹ͓͍ͯ෼ղ͕༰қʹͳΔ 52
  24. ΋ͬͱকདྷ ˞ΠϝʔδͰ͢ String doSomethingForEach(Memo memo) { return switch (memo) {

    case null -> "nullが来た"; case Text(Id id, Title title, Body body) -> doSomethingForText(body); case TodoList(Id id Title title, List<Task> tasks) && 0 < tasks().size() -> doSomethingForTodoList(tasks); case TodoList _ -> "ToDoが空っぽ"; case Image(Id id, Title title, ImageUrl url, Body body) -> doSomethingForImage(url, body); }; } 53
  25. +BWBͰͷখ͞ͳมߋ case TodoList td when 0 < td.tasks().size() -> doSomethingForTodoList(td);

    • Ψʔυύλʔϯͷࢦఆ͕ Ͱ͸ͳ͘ XIFOʹมΘΔ • ෼͔Γ΍͢͞ͷͨΊΒ͍͠ 55
  26. +BWBύλʔϯϚονϯάมભ         ϓϨϏϡʔ

    ਖ਼ࣜϦϦʔε TXJUDIࣜ JOTUBODFPG ύλʔϯϚον Ϩίʔυ γʔϧυ TXJUDI ύλʔϯϚον Ϩίʔυ ύλʔϯ 56
  27. ࠷৽ͷ৘ใΛಘΔʹ͸ • +&1 +%,&OIBODFNFOU1SPQPTBM ͰϩʔυϚοϓΛ֬ೝͰ͖Δ • ྫTXJUDIࣜ͸+&1  +&1ͦͷ΋ͷͷઆ໌͸+&1 

    • 0QFO+%,ͷެࣜϖʔδΛݟΔͱ෼͔Γ΍͍͢ • IUUQTPQFOKELKBWBOFUQSPKFDUTKEL • ໨తɾ࢓༷΍࢖͍ํɾલճͱͷࠩ෼͕؆ܿʹॻ͔Ε͍ͯΔ • 'VUVSFXPSLΛಡΉͱ͞ΒͳΔల๬͕෼͔Δ • +&1ಉ࢜ͷؔΘΓ͕௫ΊΔ 58
  28. ࠷ޙʹҰൠ+BWBϢʔβʔ໨ઢͰʜ • ௥͏ͷ͸େม • ͲΜͲΜ৽͍͠όʔδϣϯ͕΍ͬͯ͘Δ • ϓϨϏϡʔػೳ΋͋ͬͯࠓԿ͕ग़དྷΔͷ͔෼͔Βͳ͍ • ԕ͘ʹ͋Δ໨త΍ҙຯΛ೺Ѳ͢Δͱ͔͑ͬͯ෼͔Γ΍͍͢ •

    +&1΍ϓϩδΣΫτ͸ϚχΞοΫͳ஌ࣝͩͱࢥ͍ͬͯΔํ΋ɺ ஌Δ͜ͱͰָʹͳΕΔ͔΋ʜʁ ʜͦ͏͸͍ͬͯ΋େมͳͷͰίϛϡχςΟ΋׆༻͠Α͏ʂ🌟 59