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

Swift Enumeration is so useful

Swift Enumeration is so useful

Bf65cc9bdf26321a7579ddca7930cd10?s=128

Shigure Shimotori

April 03, 2018
Tweet

Transcript

  1. Swiftͷྻڍܕ͸ڧ͍(ཁग़య) ROPPONGI.swift ୈ2ճ
 @S_Shimotori

  2. ͩΕ S_Shimotori (@S_Shimotori_pub) • ࣾձਓ2೔໨ • DroidKaigi 2018ͷNOCʹ͍ͨ • ٕज़ॻయʹߦ͖͍ͨؾ࣋ͪͰ͍ͬͺ͍

  3. Swiftͷྻڍܕ͸ڧ͍ʂʂʂ

  4. Swiftͷྻڍܕ͸ڧ͍ʂʂʂ ຊ౰ʹʁ

  5. enum Day: Int { case sun = 1, mon, tue,

    wed, thu, fri, sat } enum class Day(val value: Int) { SUN(0), MON(1), TUE(2), WED(3), THU(4), FRI(5), SAT(6) } ʲDay.swiftʳ ʲDay.ktʳ Swiftؔ܎ͳ͘ͳ͍ʁ
  6. ͦ΋ͦ΋Swiftͷྻڍܕͱ͸ • Raw Valuesͳྻڍܕ • ࣄલʹಉ͡ܕͷఆ਺Λ༩͓͑ͯ͘ • Ұൠతʹྻڍܕenumͱݺ͹ΕΔ΄͏ • Associated

    Valuesͳྻڍܕ • ม਺΍ఆ਺Λ࡞ͬͨ࣌ʹݸผʹ஋Λ༻ҙ͢Δ • Ұൠతʹ୅਺తσʔλܕͱݺ͹ΕΔ΄͏
  7. enum Day: Int { case sun = 1, mon, tue,

    wed, thu, fri, sat } ʲDay.swiftʳ ʲBarcode.swiftʳ enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String) }
  8. Raw Values͸ڧ͍ʁ

  9. ྻڍܕ(Raw Values)ͷಛ௕ • IntҎ֎ͷܕͷ஋΋ఆ਺ͱͯ࣋ͭ͜͠ͱ͕Ͱ͖Δ • C/C++/C#Ͱ͸ʮҰ࿈ͷ໊લ෇͖੔਺ఆ਺ʯͰ͔͠ͳ͍ • switchͰશྻڍࢠΛ໢ཏͤ͞Δ • ඞཁͳ΋ͷ͸ࣗಈతʹ༻ҙ͞ΕΔ

    • Int΍Stringͷ࣌͸ఆ਺஋Λ͓೚ͤʹͰ͖Δ • protocol RawRepresentable΁ͷద߹ • .rawValue΍ΠχγϟϥΠβͳͲ
  10. ʲswiftc -print-ast Day.swiftʳ internal enum Day : Int { case

    sun, mon, tue, wed, thu, fri, sat internal typealias RawValue = Int internal var hashValue: Int { get } internal init?(rawValue: Int) internal var rawValue: Int { get } }
  11. Associated Values͸ڧ͍ʁ

  12. ʲBarcode.swiftʳ enum Barcode { case upc(Int, Int, Int, Int) case

    qrCode(String) }
  13. ʲBarcode.swiftʳ enum Barcode { case upc(Int, Int, Int, Int) case

    qrCode(String) } ௚࿨ ௚ੵ ௚ੵ
  14. indirect enum List<T> { case cons(T, List<T>) case `nil` }

    ʲList.swiftʳ
  15. ୅਺తσʔλܕΛදݱͰ͖Δݴޠ

  16. ୅਺తσʔλܕΛදݱͰ͖Δݴޠ

  17. ʲswiftc -dump-ast Barcode.swiftʳ※Ұ෦লུ (source_file (enum_decl "Barcode" interface type='Barcode.Type' access=internal @_fixed_layout

    (enum_case_decl (enum_element_decl "qrCode" interface type='(Barcode.Type) -> (String) -> Barcode' access=internal)) (enum_element_decl "qrCode" interface type='(Barcode.Type) -> (String) -> Barcode' access=internal)))
  18. enum Barcode { case upc(Int, Int, Int, Int) case qrCode(String)

    static func qrCode(_ str: String) -> Barcode { return Barcode.qrCode(str) } } sealed class Barcode { data class QRCode(val str: String): Barcode() companion object { fun QRCode(str: String): Barcode { return Barcode.QRCode(str) } } } ʲBarcode.swiftʳ ʲBarcode.ktʳ
  19. ͍Ζ͍ΖͳOptionalܕ

  20. ʲstdlib/public/core/Optional.swiftʳ※Ұ෦লུ public enum Optional<Wrapped> { case none case some(Wrapped) }

    pub enum Option<T> { None, Some(T), } ʲsrc/libcore/option.rsʳ※Ұ෦লུ
  21. let s = "foo"; s = null; // error let

    sn: string | null = "bar"; sn = null; // ok ʲnullable.ts with —strictNullChecks ʳ
  22. ʲshape.tsʳ interface Square { kind: "square"; size: number; } interface

    Rectangle { kind: "rectangle"; width: number; height: number; } interface Circle { kind: "circle"; radius: number; } type Shape = Square | Rectangle | Circle;
  23. sealed abstract class Option[+A] { ུ } final case class

    Some[+A](value: A) extends Option[A] { ུ } case object None extends Option[Nothing] { ུ } ʲsrc/library/scala/Option.scalaʳ※Ұ෦লུ
  24. sealed abstract class Option[+A] { ུ } final case class

    Some[+A](value: A) extends Option[A] { ུ } case object None extends Option[Nothing] { ུ } ʲsrc/library/scala/Option.scalaʳ※Ұ෦লུ
  25. ૯শܕͱڞม A͕Bͷ೿ੜܕͷͱ͖ɺWrapper<A>͕Wrapper<B>ͷ
 ೿ੜܕʹͳΔͳΒWrapper͸ڞม • Wrapper<B>ܕม਺ʹWrapper<A>ܕͷ΋ͷΛ୅ೖͰ͖ͦ͏ʂ • Scala Wrapper[A]ͷWrapper͸ඇมɺ[+A]ʹ͢Δͱڞม class Animal

    {} class Cat extends Animal {} val cat: Option[Cat] = Some(new Cat()) // Optional[+A]ͳͷͰOK val animal: Option[Animal] = cat
  26. class Animal {} class Cat: Animal {} let cat0: Optional<Cat>

    = .some(Cat()) let animal0: Optional<Animal> = cat0 enum MyOptional<T> { case some(T) case none } let cat1: MyOptional<Cat> = .some(Cat()) let animal1: MyOptional<Animal> = cat1
  27. class Animal {} class Cat: Animal {} let cat0: Optional<Cat>

    = .some(Cat()) let animal0: Optional<Animal> = cat0 enum MyOptional<T> { case some(T) case none } let cat1: MyOptional<Cat> = .some(Cat()) let animal1: MyOptional<Animal> = cat1 cannot convert value of type 'MyOptional<Cat>' to specified type 'MyOptional<Animal>'
  28. ·ͱΊ • ྻڍܕͦͷ΋ͷ͕ઐചಛڐ͔ͩΒڧ͍ɺͱ͸ݴ͑ͳ͍ • ଞͷݴޠʹ΋ྻڍܕ΍୅਺తσʔλܕ͸͋Δ • ҎԼͷ࢓༷͸SwiftͷྻڍܕΛڧ͘͢Δ͔ʁ • ྻڍܕͱ୅਺తσʔλܕ͕ͪ͝Όࠞͥ •

    ྆छڞʹswitchͰ͸໢ཏ͠ͳ͚Ε͹ͳΒͳ͍ • ඞཁͳϝιουΛิ׬ • Optionalܕͷಛผѻ͍ •
  29. ࢀߟจݙ The Swift Programming Language (Swift 4.1): Enumerations <https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html> RawRepresentable

    - Swift Standard Library | Apple Developer Documentation <https://developer.apple.com/documentation/swift/rawrepresentable> SwiftͷOptional͸ͨͩͷenum? - Qiita <https://qiita.com/koher/items/5dd6ce7427e7ecff424> SwiftͷArray͕ϛϡʔλϒϧͰ΋Covariantͳཧ༝ - Qiita <https://qiita.com/koher/items/81767a345d5ed53c04b7>
  30. ࢀߟจݙ Chapter 6. Variants / Real World OCaml <https://realworldocaml.org/v1/en/html/variants.html> ιϑτ΢ΣΞٕ๏:

    No.6 (௚ੵܕͱ୅਺తσʔλܕ) <http://www.cs.tsukuba.ac.jp/~kam/lecture/fp2016/6.pdf> C ྻڍମͷએݴ <https://msdn.microsoft.com/ja-jp/library/whbyts4t.aspx> ྻڍܕ [C++] <https://msdn.microsoft.com/ja-jp/library/2dzy4k6e.aspx> ྻڍܕ (C# ϓϩάϥϛϯά ΨΠυ) | Microsoft Docs <https://docs.microsoft.com/ja-jp/dotnet/csharp/programming-guide/enumeration-types> Enum Classes - Kotlin Programming Language <https://kotlinlang.org/docs/reference/enum-classes.html> Effective Scala <http://twitter.github.io/effectivescala/index-ja.html> ྻڍܕ (enum) ͕ཉ͍͠ͱ͖ͷ Enumeration ͱ case object... - tnoda-scala <http://tnoda-scala.tumblr.com/post/106430183326/%E5%88%97%E6%8C%99%E5%9E%8B-enum- %E3%81%8C%E6%AC%B2%E3%81%97%E3%81%84%E3%81%A8%E3%81%8D%E3%81%AE-enumeration-%E3%81%A8-case-object> ܕύϥϝʔλͱมҐࢦఆ · ScalaݚमςΩετ <https://dwango.github.io/scala_text/type-parameter.html>
  31. ࢀߟจݙ Advanced Types - TypeScript <http://www.typescriptlang.org/docs/handbook/advanced-types.html> Union Types͸௚࿨ܕͰ͸ͳ͍ | ࡶهா

    <https://blog.miz-ar.info/2015/01/union-types/> TypeScriptɺ͓લ΋͔ɿ null΍undefinedͷѻ͍͕ΠΠΧήϯա͗ʲࣄ࣮ޡೝ͋Γʳ - ᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه <http://d.hatena.ne.jp/m-hiyama/20180112/1515717324> TypeScriptɺ๻͕ѱ͔ͬͨɺΰϝϯɿ null΍undefinedͷѻ͍͸ϚτϞͩͬͨ - ᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه <http://d.hatena.ne.jp/m-hiyama/20180112/1515750208>