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

Swift Enumeration is so useful

Swift Enumeration is so useful

Shigure Shimotori

April 03, 2018
Tweet

More Decks by Shigure Shimotori

Other Decks in Programming

Transcript

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

    @S_Shimotori

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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ؔ܎ͳ͘ͳ͍ʁ

    View Slide

  6. ͦ΋ͦ΋Swiftͷྻڍܕͱ͸
    • Raw Valuesͳྻڍܕ
    • ࣄલʹಉ͡ܕͷఆ਺Λ༩͓͑ͯ͘
    • Ұൠతʹྻڍܕenumͱݺ͹ΕΔ΄͏
    • Associated Valuesͳྻڍܕ
    • ม਺΍ఆ਺Λ࡞ͬͨ࣌ʹݸผʹ஋Λ༻ҙ͢Δ
    • Ұൠతʹ୅਺తσʔλܕͱݺ͹ΕΔ΄͏

    View Slide

  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)
    }

    View Slide

  8. Raw Values͸ڧ͍ʁ

    View Slide

  9. ྻڍܕ(Raw Values)ͷಛ௕
    • IntҎ֎ͷܕͷ஋΋ఆ਺ͱͯ࣋ͭ͜͠ͱ͕Ͱ͖Δ
    • C/C++/C#Ͱ͸ʮҰ࿈ͷ໊લ෇͖੔਺ఆ਺ʯͰ͔͠ͳ͍
    • switchͰશྻڍࢠΛ໢ཏͤ͞Δ
    • ඞཁͳ΋ͷ͸ࣗಈతʹ༻ҙ͞ΕΔ
    • Int΍Stringͷ࣌͸ఆ਺஋Λ͓೚ͤʹͰ͖Δ
    • protocol RawRepresentable΁ͷద߹
    • .rawValue΍ΠχγϟϥΠβͳͲ

    View Slide

  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 }
    }

    View Slide

  11. Associated Values͸ڧ͍ʁ

    View Slide

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

    View Slide

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

    View Slide

  14. indirect enum List {
    case cons(T, List)
    case `nil`
    }
    ʲList.swiftʳ

    View Slide

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

    View Slide

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




    View Slide

  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)))

    View Slide

  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ʳ

    View Slide

  19. ͍Ζ͍ΖͳOptionalܕ

    View Slide

  20. ʲstdlib/public/core/Optional.swiftʳ※Ұ෦লུ
    public enum Optional {
    case none
    case some(Wrapped)
    }
    pub enum Option {
    None,
    Some(T),
    }
    ʲsrc/libcore/option.rsʳ※Ұ෦লུ

    View Slide

  21. let s = "foo";
    s = null; // error
    let sn: string | null = "bar";
    sn = null; // ok
    ʲnullable.ts with —strictNullChecks ʳ

    View Slide

  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;

    View Slide

  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ʳ※Ұ෦লུ

    View Slide

  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ʳ※Ұ෦লུ


    View Slide

  25. ૯শܕͱڞม
    A͕Bͷ೿ੜܕͷͱ͖ɺWrapper͕Wrapperͷ

    ೿ੜܕʹͳΔͳΒWrapper͸ڞม
    • Wrapperܕม਺ʹWrapperܕͷ΋ͷΛ୅ೖͰ͖ͦ͏ʂ
    • 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

    View Slide

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

    View Slide

  27. class Animal {}
    class Cat: Animal {}
    let cat0: Optional = .some(Cat())
    let animal0: Optional = cat0
    enum MyOptional {
    case some(T)
    case none
    }
    let cat1: MyOptional = .some(Cat())
    let animal1: MyOptional = cat1
    cannot convert value of type 'MyOptional' to specified type 'MyOptional'

    View Slide

  28. ·ͱΊ
    • ྻڍܕͦͷ΋ͷ͕ઐചಛڐ͔ͩΒڧ͍ɺͱ͸ݴ͑ͳ͍
    • ଞͷݴޠʹ΋ྻڍܕ΍୅਺తσʔλܕ͸͋Δ
    • ҎԼͷ࢓༷͸SwiftͷྻڍܕΛڧ͘͢Δ͔ʁ
    • ྻڍܕͱ୅਺తσʔλܕ͕ͪ͝Όࠞͥ
    • ྆छڞʹswitchͰ͸໢ཏ͠ͳ͚Ε͹ͳΒͳ͍
    • ඞཁͳϝιουΛิ׬
    • Optionalܕͷಛผѻ͍

    View Slide

  29. ࢀߟจݙ
    The Swift Programming Language (Swift 4.1): Enumerations

    RawRepresentable - Swift Standard Library | Apple Developer Documentation

    SwiftͷOptional͸ͨͩͷenum? - Qiita

    SwiftͷArray͕ϛϡʔλϒϧͰ΋Covariantͳཧ༝ - Qiita

    View Slide

  30. ࢀߟจݙ
    Chapter 6. Variants / Real World OCaml

    ιϑτ΢ΣΞٕ๏: No.6 (௚ੵܕͱ୅਺తσʔλܕ)

    C ྻڍମͷએݴ

    ྻڍܕ [C++]

    ྻڍܕ (C# ϓϩάϥϛϯά ΨΠυ) | Microsoft Docs

    Enum Classes - Kotlin Programming Language

    Effective Scala

    ྻڍܕ (enum) ͕ཉ͍͠ͱ͖ͷ Enumeration ͱ case object... - tnoda-scala
    %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ݚमςΩετ

    View Slide

  31. ࢀߟจݙ
    Advanced Types - TypeScript

    Union Types͸௚࿨ܕͰ͸ͳ͍ | ࡶهா

    TypeScriptɺ͓લ΋͔ɿ null΍undefinedͷѻ͍͕ΠΠΧήϯա͗ʲࣄ࣮ޡೝ͋Γʳ - ᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه

    TypeScriptɺ๻͕ѱ͔ͬͨɺΰϝϯɿ null΍undefinedͷѻ͍͸ϚτϞͩͬͨ - ᐻࢁਖ਼޾ͷΩϚΠϥࣂҭه

    View Slide