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

Opaque Result Typeの解説

Opaque Result Typeの解説

omochimetaru

May 24, 2019
Tweet

More Decks by omochimetaru

Other Decks in Programming

Transcript

  1. ܕͷਖ਼͠͞Λଛͳ͏ let a = AnyCollection<String>(["hello"]) let b = AnyCollection<Character>("hello") let

    c = b[a.startIndex] ࣮ߦ࣌(!)Τϥʔ Collection.Indexʹؔ͢Δܕ৘ใ͕ࣦΘΕ͍ͯ Δ 11
  2. ॻ͖ࠐΈ 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
  3. 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
  4. ͕͜͜ϛι 2ͭͷsome P͸ҧ͏ܕ func f1() -> some P func f2()

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

    ResultOfF1 struct ResultOfF2 : P {} func f2() -> ResultOfF2 var a = f1() let b = f2() a = b // ίϯύΠϧΤϥʔ 20
  6. ܕ৘ใͷอ࣋ 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
  7. ॻ͖ࠐΈՄೳ 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
  8. Reverse GenericsΛ࢖͏ͱɺදݱྗ্͕͕Δ // 2ͭͷsome Pͷؔ࿈Λॻ͚ͳ͍ func makePS() -> (some P,

    some P) // 2ͭͷʮಉҰͷʯPΛຬͨ͢ܕ func makePS() -> <X: P> (X, X) 33
  9. // PͰ͋ΔਅͷܕΛड͚औΔ func useSomeP(_ x: some P) // PͰ͋ΔਅͷܕXΛड͚औΔ func

    useGeneP<X: P>(_ x: X) ಄ͷதͰҰ౓XΛهԱ͠ͳ͍ͷͰಡΈ΍͍͢ɺྲྀ ߦΓͦ͏ 37