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

Opaque Result Typeの解説

Opaque Result Typeの解説

3781f49ea2c76d6ecf0c6cda46096d49?s=128

omochimetaru

May 24, 2019
Tweet

More Decks by omochimetaru

Other Decks in Programming

Transcript

  1. Opaque Result Typeͷղઆ omochimetaru Θ͍Θ͍swiftc #11 1

  2. 5ඵͳΒ 2

  3. ͜ΕΛݟͯ Proposal SE-0244 Opaque Result Type https://github.com/apple/swift-evolution/blob/ master/proposals/0244-opaque-result-types.md 3

  4. ͜ΕΛݟͯ Swift 5.1 ʹಋೖ͞ΕΔ Opaque Result Type ͱ͸ Կ͔ https://qiita.com/koher/items/

    338d2f2d0c4731e3508f 4
  5. ղઆ͢ΔͳΒ 5

  6. Opaque Result Type ύϑΥʔϚϯεΛอͪͭͭɺ ਅͷܕΛӅṭ͢Δ৽ػೳ 6

  7. ಈػ 7

  8. ਅͷܕΛӅṭ ʮ͋ΔϓϩτίϧΛฦ͢ʯͱݴ͍͍ͨ͜ͱ͕͋Δ func loadUsers() -> ??? // Sequence of User

    8
  9. ۩ମతͳܕΛฦͯ͠͠·͏ͱকདྷมߋͰ͖ͳ͍ // ver 1.0.0 func loadUsers() -> [User] // ver

    1.1.0 func loadUsers() -> LazySequence<User> 9
  10. ैདྷͷํ๏ Type Erasure΍ExistentialͰฦ͢ func loadUsers() -> AnySequence<User> • ܕͷਖ਼͠͞Λଛͳ͏ •

    ύϑΥʔϚϯεΛଛͳ͏ 10
  11. ܕͷਖ਼͠͞Λଛͳ͏ let a = AnyCollection<String>(["hello"]) let b = AnyCollection<Character>("hello") let

    c = b[a.startIndex] ࣮ߦ࣌(!)Τϥʔ Collection.Indexʹؔ͢Δܕ৘ใ͕ࣦΘΕ͍ͯ Δ 11
  12. ॻ͖ࠐΈ protocol P {} extension Int : P {} struct

    Container { var value: P { get { return _value } set { _value = newValue as! Int } } var _value: Int = 1 } ࣮࣭తʹsuper typeͳͷͰɺcontravarianceϙδγϣϯͰյ ΕΔ 12
  13. ύϑΥʔϚϯεΛଛͳ͏ ϥούʔʹแ·ΕͯΔ෼஗͍ 13

  14. ղܾํ๏ 14

  15. Opaque Result Type some P ʮϓϩτίϧPΛຬͨ͢ԿΒ͔ͷܕʯΛද͢ه๏ 15

  16. ؔ਺ͷฦΓ஋Ͱ࢖͑Δ extension Int : P {} func getP() -> some

    P { return 3 } 16
  17. ϓϩύςΟͰ࢖͑Δ var propertyP: some P { get { ... }

    set { ... } } 17
  18. associated type͕͍ͭͯͯ΋࢖͑Δ func numbers() -> some MutableCollection { return [1,

    2, 3] } var xs = numbers() let i0 = xs.startIndex let i1 = xs.index(after: i0) xs[i0] = xs[i1] print(xs) // [2, 2, 3] 18
  19. ͕͜͜ϛι 2ͭͷsome P͸ҧ͏ܕ func f1() -> some P func f2()

    -> some P var a = f1() let b = f2() a = b // ίϯύΠϧΤϥʔ 19
  20. ORT͸ؔ਺୯ҐͷΞΠσϯςΟςΟΛ࣋ͭ ԼهͷΑ͏ͳঢ়ଶΛ૝૾͢Δͱྑ͍ struct ResultOfF1 : P {} func f1() ->

    ResultOfF1 struct ResultOfF2 : P {} func f2() -> ResultOfF2 var a = f1() let b = f2() a = b // ίϯύΠϧΤϥʔ 20
  21. ORTͷந৅Խදݱ some Pͱͯ͠ҙຯ෇͚͞ΕΔͨΊɺকདྷͷόʔ δϣϯͰਅͷܕΛมߋͰ͖Δ ιʔεޓ׵ੑ͚ͩͰ͸ͳ͘ɺόΠφϦޓ׵ੑ͕͋ Γɺ࠶ίϯύΠϧͤͣʹมߋͰ͖Δ // ver 1.0.0 func

    f() -> some P { return 3 } // ver 1.1.0 func f() -> some P { return "3" } 21
  22. ໰୊ͷղܾΛΈ͍ͯ͘ • ܕ৘ใͷอ࣋ • ॻ͖ࠐΈ • ύϑΥʔϚϯε 22

  23. ܕ৘ใͷอ࣋ func numbers() -> some Collection { return [1, 2,

    3] } func message() -> some Collection { return "hello" } let a = numbers() let b = message() let x = a[b.startIndex] // ܕΤϥʔ 23
  24. ॻ͖ࠐΈՄೳ struct S { var value: some P { get

    { return _value } set { _value = newValue as! Int } } var _value: Int = 3 } var s = S() let x = s.value s.value = x // OK s.value = Int(1) // Error s.value = "hello" // Error 24
  25. ύϑΥʔϚϯε ίϯύΠϥ͕ਅͷܕΛ஌͍ͬͯΔ࣌͸ɺ࠷దԽʹ ΑΓਅͷܕͱͯ͠ѻ͏ίʔυʹಛघԽ͞ΕΔͨ ΊɺෆཁͳύϑΥʔϚϯεϩε͕ແ͍ 25

  26. ʮ࠷దԽͰ͖Δͱ͖͸଎͍ʯͱ͍͏ҙຯͰ͸ Existentialͱಉ͡ɻԿ͕ҧ͏ͷ͔ʁ Existential͸super typeͰ͋ΔͨΊɺʮଞͷܕ͕ ೖ͍ͬͯΔՄೳੑ͕ͳ͍ʯূ໌͕ඞཁɻ ORT͸ʮ஌Βͳ͍͋ΔܕʯͳͷͰɺͦͷূ໌͸ෆ ཁɻ୯ʹͦͷਅͷܕΛ஌Ε͹ྑ͍͚ͩɻ ݁Ռͱͯ͠ಉҰϞδϡʔϧͳΒৗʹ࠷దԽՄೳɻ 26

  27. ൃల 27

  28. ORTͷಋೖʹ͋ͨͬͯٞ࿦͕֦େ͍ͯͬͨ͠ɻ ऩरΛ͚ͭΔͨΊ৽͍͠ϚχϑΣετ͕࡞ΒΕ ͨɻ Improving the UI of generics https://forums.swift.org/t/improving-the-ui-of- generics/22814

    Generic Manifestoʹଓ͘ࢦ਑ 28
  29. ORT͸δΣωϦΫεͱࣅ͍ͯΔ // PͰ͋ΔਅͷܕΛฦ͢ func makeSomeP() -> some P // PͰ͋ΔਅͷܕXΛड͚औΔ

    func useGeneP<X: P>(_ x: X) 29
  30. ͜Εͱ͸ҧ͏ // PͰ͋ΔਅͷܕXΛฦ͢ func makeGeneP<X: P>() -> X ͜ͷX͸ݺͼग़͠ଆ͕ܾΊΔ some

    P͸ݺͼग़͞Εଆ͕ܾΊΔ 30
  31. Reverse Generics ݺͼग़͞Εଆ͕ܾΊΔδΣωϦοΫύϥϝʔλͱ ͍͏৽֓೦Λߟ͑ͯΈΔ func makeRevGeneP() -> <X: P> X

    31
  32. ORT͸Reverse GenericͷsugarͱΈͳͤΔ // PͰ͋ΔਅͷܕΛฦ͢ func makeSomeP() -> some P //

    PͰ͋ΔਅͷܕXΛฦ͢ func makeRevGeneP() -> <X: P> X 32
  33. Reverse GenericsΛ࢖͏ͱɺදݱྗ্͕͕Δ // 2ͭͷsome Pͷؔ࿈Λॻ͚ͳ͍ func makePS() -> (some P,

    some P) // 2ͭͷʮಉҰͷʯPΛຬͨ͢ܕ func makePS() -> <X: P> (X, X) 33
  34. ORTߏจͷ֦ுҊ func makePS() -> (some P A, some P B)

    where A == B 34
  35. Opaque Result Type → Reverse Generics ?? → Generics 35

  36. Opaque Argument Type ΋͠someΛҾ਺෦෼ʹॻ͚ͨΒɺGenericsͷ sugarͱΈͳͤΔ // PͰ͋ΔਅͷܕΛड͚औΔ func useSomeP(_ x:

    some P) // PͰ͋ΔਅͷܕXΛड͚औΔ func useGeneP<X: P>(_ x: X) 36
  37. // PͰ͋ΔਅͷܕΛड͚औΔ func useSomeP(_ x: some P) // PͰ͋ΔਅͷܕXΛड͚औΔ func

    useGeneP<X: P>(_ x: X) ಄ͷதͰҰ౓XΛهԱ͠ͳ͍ͷͰಡΈ΍͍͢ɺྲྀ ߦΓͦ͏ 37
  38. ORTͱExistential Α͘ࣅ͍ͯΔ // PΛຬͨ͢**ͱ͋Δ**ܕΛฦ͢ func makeSomeP() -> some P //

    PΛຬͨ͢**ͲΜͳ**ܕͰ΋୅ೖͰ͖Δ var b: P = ... 38
  39. Existentialͷanyه๏ // PΛຬͨ͢**ͱ͋Δ**ܕΛฦ͢ func makeSomeP() -> some P // PΛຬͨ͢**ͲΜͳ**ܕͰ΋୅ೖͰ͖Δ

    var b: any P ରশతͰ͖Ε͍ 39
  40. Protocol੍໿ͱExistentialͷྨࣅ ॳڃऀࡴ͠ // P͸੍໿ func useGeneP<X: P>(_ x: X) //

    P͸Existential func useAnyP(_ x: P) 40
  41. ҧ͏ػೳ͸ҧ͏ه๏ͷ΄͏͕Θ͔Γ΍͍͢ // P͸੍໿ func useGeneP<X: P>(_ x: X) // P͸Existential

    func useAnyP(_ x: any P) 41