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

Swiftの型システムに入門する - iOSDC Japan 2018

ukitaka
August 31, 2018

Swiftの型システムに入門する - iOSDC Japan 2018

ukitaka

August 31, 2018
Tweet

More Decks by ukitaka

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ Yuki Takahashi @ukitaka ▸ iOS Engineer @ Mercari Fukuoka

    Office ▸ SwiftͷίϯύΠϥɺಛʹܕγεςϜʹڵຯ͕͋Γ·͢ɻ ▸ ͨ·ʹϒϩά΍QiitaΛॻ͍ͯ·͢ɻ
  2. SwiftͷܕγεςϜͷಛ௃Λ஌Δ ଘࡏܕ (Existential Type) ▸ = ܕͱͯ͠ͷprotocol ▸ Swift͸ଘࡏܕΛ͋Δҙຯ௚઀తʹѻ͑Δ௝͍͠ ݴޠ

    ▸ ଘࡏܕͩͱҙࣝͤͣʹ࢖͑Δݴޠઃܭ ▸ Opening existential౳͕ಋೖ͞ΕΔͱଘࡏܕͱ ͸ͳʹ͔Λ஌Βͳ͍ͱཧղͮ͠Β͍͜ͱ΋͋Δ ͔΋͠Εͳ͍ → গͳ͘ͱ΋protocolܕ = ଘࡏܕͰ͋Δͱ͍͏͜ͱΛ ಄ͷย۱ʹஔ͍͓ͯ͘ͱྑ͍
  3. SwiftͷܕγεςϜͷಛ௃Λ஌Δ ܕΫϥεɾݶఆܕ(Qualified Type) ▸ = ܕύϥϝʔλͷ੍໿ͱͯ͠ͷprotocol ▸ T: Equatable ▸

    Conditional Conformance ▸ ݶఆܕ͸ʮܕΫϥεΛܕγεςϜͰѻ͏ʯͨΊͷ ཧ࿦ ▸ ܕ্ͷड़ޠʹΑ੍ͬͯ໿Λ͔͚Δ ▸ Jones, Mark P. Qualified Types: Theory and Practice. Cambridge University Press, 1994.
  4. SwiftͷܕγεςϜͷಛ௃Λ஌Δ SwiftͷܕγεςϜ͕͍࣋ͬͯͳ͍΋ͷ (ൈਮ) ❌ ׬શͳܕਪ࿦ ❌ ߴΧΠϯυଟ૬ (Haskell, ScalaͳͲ) ❌

    Rank2Ҏ্ͷଟ૬ ❌ ೚ҙͷδΣωϦοΫͳܕʹର͢Δڞมɾ൓มͷࢦఆ → Կ͔ͱͷτϨʔυΦϑͳͷͰɺ͋Δ͔Βྑ͍ͱ͍͏Θ͚Ͱ΋ͳ͍ Swift͸͜ͷลΓͷόϥϯε͕ͱΕ͍ͯΔྑ͍ݴޠ
  5. Swiftͷܕਪ࿦ͱͦͷσόοάํ๏Λ஌Δ ASTΛग़ྗ͢ΔίϯύΠϥΦϓγϣϯ2ͭ % swiftc -help OVERVIEW: Swift compiler USAGE: swiftc

    [options] <inputs> MODES: -dump-ast Parse and type-check input file(s) and dump AST(s) -dump-parse Parse input file(s) and dump AST(s) Swift AST AST (typed) -dump-parse -dump-ast lib/Sema
  6. Swiftͷܕਪ࿦ͱͦͷσόοάํ๏Λ஌Δ ASTΛग़ྗ͢ΔίϯύΠϥΦϓγϣϯ2ͭ % swiftc -help OVERVIEW: Swift compiler USAGE: swiftc

    [options] <inputs> MODES: -dump-ast Parse and type-check input file(s) and dump AST(s) -dump-parse Parse input file(s) and dump AST(s) Swift AST AST (typed) ஌Γ͍ͨͷ͸͜ͷաఔͷdumpํ๏ lib/Sema
  7. SwiftͷܕγεςϜͷ࣮૷ΛಡΉ ConstraintSystemͷ࢓ࣄ - ੍໿ੜ੒ ▸ createTypeVariableͰܕม਺Λ࡞Δ ▸ addConstraintͰ੍໿Λੜ੒ˍ؅ཧ͢ Δ ▸

    ConstraintKindͰ੍໿ͷछྨΛࢦఆ ▸ ੍໿͸ConstraintGraphͱݺ͹ΕΔά ϥϑͰ؅ཧ ੍໿ੜ੒ͷྫ
  8. SwiftͷܕγεςϜͷཧ࿦Λ஌Δ ܕγεςϜೖ໳(TaPL) ▸ Types and Programming Language (TaPL) ▸ ܕͳ͠ͷϥϜμܭࢉͷΑ͏ͳجૅతͳͱ͜Ζ͔

    Βঃʑʹݱ୅ͷϓϩάϥϛϯάݴޠ͕࣋ͭΑ͏ ͳػೳΛ෇͚Ճ͍͑ͯ͘ ▸ 1ষ͸ແྉͰެ։͞Ε͍ͯΔʂʮܕʯͱ͸Կ͔Λ ஌Δʹ͸࠷ߴͷࢿྉ → SwiftΛ஌ΔͨΊʹ͸Ͳ͏ಡΊ͹ྑ͍ʁ
  9. SwiftͷܕγεςϜͷཧ࿦Λ஌Δ TaPLͷา͖ํ ▸ લఏͱͯ͠ɺ͢΂ͯͷষΛಡΉඞཁ͸ͳ͍ ▸ 9ষ·Ͱ͸جૅͱͯ͠ಡΜͰ͓͘ͱΑͦ͞͏ ▸ લ൒͸ʮཧ࿦ͷষ → OCamlʹΑΔ࣮૷ͷষʯͷ

    ܁Γฦ͠ͳͷͰ࣮૷ΛSwiftͰ΍ͬͯΈΔͱ͍͏ ͷ͸ྑ͍ख ▸ ઌ΄ͲݟͨΑ͏ͳSwiftͷܕγεςϜͷಛ௃Λ ѻ͍ͬͯΔͷ͸15ষɺ22ষɺ24ষ͋ͨΓ ܕγεςϜೖ໳−ϓϩάϥϛϯάݴޠͱܕͷཧ࿦− ংจΑΓҾ༻
  10. SwiftͷܕγεςϜͷཧ࿦Λ஌Δ ୈ22ষ ܕ࠶ߏங ▸ ܕ࠶ߏங = ܕਪ࿦ ▸ ܕਪ࿦ͱSwift͕࣋ͭΑ͏ͳδΣωϦΫεʹ͍ͭ ͯऔΓѻ͍ͬͯΔষ

    ▸ SwiftͷδΣωϦοΫͳؔ਺͸ܕਪ࿦ͱηοτͰ ఏڙ͞Ε͍ͯΔػೳ (letଟ૬) ͱ͍͏͜ͱ͕Θ͔ Δ The secret life of types in Swift https://medium.com/@slavapestov/the-secret-life-of-types-in-swift-ff83c3c000a5 ܕγεςϜೖ໳−ϓϩάϥϛϯάݴޠͱܕͷཧ࿦− ୈ22ষʮܕ࠶ߏஙʯΑΓҾ༻
  11. SwiftͷܕγεςϜͷཧ࿦Λ஌Δ ୈ15ষ ෦෼ܕ෇͚ ▸ ෦෼ܕ෇͚ = αϒλΠϐϯά ▸ ͋Δܕͱ͋Δܕ͕αϒλΠϓؔ܎Ͱ͋Δ͔Ͳ͏ ͔͸ɺͦͷܕγεςϜͰͷʮܾΊʯͷ໰୊ͩͱ

    ͍͏͜ͱ͕Θ͔Ε͹Αͦ͞͏ ▸ 15.5અͷʮܕڧ੍ҙຯ࿦ʯͷͱ͜Ζ͸·͞ʹ SwiftίϯύΠϥͰߦΘΕ͍ͯΔ͜ͱ͕ॻ͍ͯ͋ ΔͷͰඞಡ ܕγεςϜೖ໳−ϓϩάϥϛϯάݴޠͱܕͷཧ࿦− ୈ15ষʮ෦෼ܕ෇͚ʯΑΓҾ༻
  12. SwiftͷܕγεςϜͷཧ࿦Λ஌Δ ୈ24ষ ଘࡏܕ ▸ λΠτϧ௨Γଘࡏܕ(Existential type)Λѻ͏ষ ▸ ଘࡏܕ͕ʮ͋ΔܕͱͦͷܕΛ࢖ͬͨܕʯͷ૊ɺ ଘࡏܕͷ஋͸ʮ͋Δܕ͕۩ମతʹͳʹ͔ +

    ͦͷ ஋ʯͷϖΞͰ͋Δͱ͍͏ͱ͜Ζ͔͑ͭ͞ΊΕ͹ Swiftͷଘࡏܕ΋ා͘ͳ͍ ϖΞͬΆ͍งғؾ ܕγεςϜೖ໳−ϓϩάϥϛϯάݴޠͱܕͷཧ࿦− ୈ24ষʮଘࡏܕʯΑΓҾ༻
  13. SwiftͷܕγεςϜͷཧ࿦Λ஌Δ Swiftʹؔ܎͋Γͦ͏Ͱؔ܎ͳ͍ষ ▸ ୈ23ষ શশܕ ▸ ͍ΘΏΔδΣωϦΫεͷ͜ͱͩͱࢥͬͯಡΉͱϋϚΔ ▸ Swift͸͜ͷষͰऔΓ্͛ΒΕ͍ͯΔΑ͏ͳλΠϓͷڧྗͳδΣωϦΫεΛ࣋ͨͳ͍ ▸

    ୈ26ষ ༗քྔԽ ▸ ͺͬͱݟ T: EquatableͷΑ͏ͳ੍໿ʹ͍ͭͯͷཧ࿦తഎܠ͕ॻ͍ͯ͋ΔΑ͏ʹΈ͑Δ͕ɺ࣮͸ؔ܎ ͳ͍ (see: ݶఆܕ) ▸ ༗քྔԽ͸αϒλΠϐϯάʹΑ੍ͬͯ໿Λ͔͚Δɺݶఆܕ͸ܕ্ͷड़ޠͰ੍໿Λ͔͚Δͱ͍͏ҧ͍