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. 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ؔ܎ͳ͘ͳ͍ʁ
  2. ͦ΋ͦ΋Swiftͷྻڍܕͱ͸ • Raw Valuesͳྻڍܕ • ࣄલʹಉ͡ܕͷఆ਺Λ༩͓͑ͯ͘ • Ұൠతʹྻڍܕenumͱݺ͹ΕΔ΄͏ • Associated

    Valuesͳྻڍܕ • ม਺΍ఆ਺Λ࡞ͬͨ࣌ʹݸผʹ஋Λ༻ҙ͢Δ • Ұൠతʹ୅਺తσʔλܕͱݺ͹ΕΔ΄͏
  3. 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) }
  4. ʲ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 } }
  5. ʲ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)))
  6. 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ʳ
  7. ʲ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ʳ※Ұ෦লུ
  8. let s = "foo"; s = null; // error let

    sn: string | null = "bar"; sn = null; // ok ʲnullable.ts with —strictNullChecks ʳ
  9. ʲ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;
  10. 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ʳ※Ұ෦লུ
  11. 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ʳ※Ұ෦লུ
  12. 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
  13. 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>'
  14. ࢀߟจݙ 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>
  15. ࢀߟจݙ 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>
  16. ࢀߟจݙ 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>