Share
Embed
Transcript
31 slides
-
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ؔͳ͘ͳ͍ʁ -
ͦͦSwiftͷྻڍܕͱ • Raw Valuesͳྻڍܕ • ࣄલʹಉ͡ܕͷఆΛ༩͓͑ͯ͘ • ҰൠతʹྻڍܕenumͱݺΕΔ΄͏ • Associated
Valuesͳྻڍܕ • มఆΛ࡞ͬͨ࣌ʹݸผʹΛ༻ҙ͢Δ • ҰൠతʹతσʔλܕͱݺΕΔ΄͏ -
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) } -
ྻڍܕ(Raw Values)ͷಛ • IntҎ֎ͷܕͷఆͱͯ࣋ͭ͜͠ͱ͕Ͱ͖Δ • C/C++/C#ͰʮҰ࿈ͷ໊લ͖ఆʯͰ͔͠ͳ͍ • switchͰશྻڍࢠΛཏͤ͞Δ • ඞཁͳͷࣗಈతʹ༻ҙ͞ΕΔ
• IntStringͷ࣌ఆΛ͓ͤʹͰ͖Δ • protocol RawRepresentableͷద߹ • .rawValueΠχγϟϥΠβͳͲ -
ʲ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 } } -
ʲBarcode.swiftʳ enum Barcode { case upc(Int, Int, Int, Int) case
qrCode(String) } -
ʲBarcode.swiftʳ enum Barcode { case upc(Int, Int, Int, Int) case
qrCode(String) } ੵ ੵ -
ʲ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))) -
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ʳ -
ʲ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ʳ※Ұ෦লུ -
let s = "foo"; s = null; // error let
sn: string | null = "bar"; sn = null; // ok ʲnullable.ts with —strictNullChecks ʳ -
ʲ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; -
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ʳ※Ұ෦লུ -
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ʳ※Ұ෦লུ -
૯শܕͱڞม 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 -
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 -
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>' -
·ͱΊ • ྻڍܕͦͷͷ͕ઐചಛڐ͔ͩΒڧ͍ɺͱݴ͑ͳ͍ • ଞͷݴޠʹྻڍܕతσʔλܕ͋Δ • ҎԼͷ༷SwiftͷྻڍܕΛڧ͘͢Δ͔ʁ • ྻڍܕͱతσʔλܕ͕ͪ͝Όࠞͥ •
྆छڞʹswitchͰཏ͠ͳ͚ΕͳΒͳ͍ • ඞཁͳϝιουΛิ • Optionalܕͷಛผѻ͍ • -
ࢀߟจݙ 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> -
ࢀߟจݙ 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> -
ࢀߟจݙ Advanced Types - TypeScript <http://www.typescriptlang.org/docs/handbook/advanced-types.html> Union TypesܕͰͳ͍ | ࡶهா
<https://blog.miz-ar.info/2015/01/union-types/> TypeScriptɺ͓લ͔ɿ nullundefinedͷѻ͍͕ΠΠΧήϯա͗ʲࣄ࣮ޡೝ͋Γʳ - ᐻࢁਖ਼ͷΩϚΠϥࣂҭه <http://d.hatena.ne.jp/m-hiyama/20180112/1515717324> TypeScriptɺ͕ѱ͔ͬͨɺΰϝϯɿ nullundefinedͷѻ͍ϚτϞͩͬͨ - ᐻࢁਖ਼ͷΩϚΠϥࣂҭه <http://d.hatena.ne.jp/m-hiyama/20180112/1515750208>