Kuniwak
October 13, 2017
2.8k

# 構造的差分ライブラリ開発時の悲劇・喜劇

October 13, 2017

## Transcript

3. ### func testExample1() { let a = Example( key1: "I'm not

changed", key2: "I'm deleted" ) let b = Example( key1: "I'm not changed", key2: "I'm inserted" ) XCTAssertEqual(a, b) } Α͋͘ΔTUSVDUͷൺֱ
4. ### XCTAssertEqual failed: ("Example(key1: "I\'m not changed", key2: "I\'m deleted")") is

not equal to ("Example(key1: "I\'m not changed", key2: "I\'m inserted")") - ςετ݁Ռ͸ඇৗʹΘ͔ΓͮΒ͍

6. ### import MirrorDiffKit func testExample2() { let a = Example( key1:

"I'm not changed", key2: "I'm deleted" ) let b = Example( key1: "I'm not changed", key2: "I'm inserted" ) XCTAssertEqual(a, b, diff(between: a, and: b)) } ࣦഊ࣌ͷϝοηʔδʹ  .JSSPS%J⒎,JUͷEJ⒎Λࢦఆ .JSSPS%J⒎,JUΛJNQPSU
7. ### struct Example { key1: "I'm not changed" - key2: "I'm

deleted" + key2: "I'm inserted" } ͜Ε͕ߏ଄తͳࠩ෼ ͙͢ʹࠩҟ͕Θ͔Γ·͢
8. ### [ "I'm not changed" "I'm not changed" - "I'm deleted"

"I'm not changed" + "I'm inserted" ] ഑ྻͷॱংมߋͳͲ΋  ߏ଄తͳࠩ෼Ͱ͢ ͜Ε΋ݟ΍͘͢දࣔ͞Ε·͢

11. ### let example = Example( key1: "I'm not changed", key2: "I'm

deleted" ) let hint = ( subject: Example.self, displayStyle: .struct, children: [ (label: "key1", value: "I'm not changed"), (label: "key2", value: "I'm deleted"), ] ) ߏ଄తͳࠩ෼ͷܭࢉʹ͸ɺ  ࣍ͷΑ͏ͳΦϒδΣΫτͷ  ߏ଄ͷώϯτ͕ෆՄܽͰ͢ ΦϒδΣΫτͷܕ ΦϒδΣΫτͷछྨ ϓϩύςΟͷ໊લͱ஋

13. ### let example = Example( key1: "I'm not changed", key2: "I'm

deleted" ) let mirror = Mirror(reflecting: example) let hint = ( subject: mirror.subjectType, displayStyle: mirror.displayStyle, children: mirror.children ) ௐ΂͍ͨΦϒδΣΫτ͔Β  .JSSPSΦϒδΣΫτΛ  ࡞੒͢Δͱɺ ܕ৘ใ΍ΦϒδΣΫτͷछྨɺ  ϓϩύςΟͷ໊લͱ஋ͳͲΛ  ೖखͰ͖·͢

18. ### struct Example { let any: Any? } let example =

Example(any: nil) if let first = Mirror(reflecting: example) .children.first { print(first.value) } ϓϩύςΟΛOJMͰॳظԽ ϓϩύςΟͷ஋ΛݟΔͱɺ  ઌ΄ͲOJMͰॳظԽͨ͠ͷͰ  OJMʹͳ͍ͬͯΔ "OZ ͷϓϩύςΟ͕͋ΔTUSVDU .JSSPSΛ࡞੒ͯ͠ɺTUSVDUͷ  ϓϩύςΟͷώϯτΛೖख
19. ### struct Example { let any: Any? } let example =

Example(any: nil) if let first = Mirror(reflecting: example) .children.first { print(first.value == nil) print(first.value) } GBMTF OJMͰ΋͔֬ΊͯΈΑ͏  ͔͠͠ೖ͍ͬͯΔͷ͸OJMʜ

25. ### let x: Any? = nil let wrapper: Any = x

// WARNING: Comparing non-optional value of // type 'Any' to nil always returns false print(x == nil) ࣮͸ղઆͨ͠ྫΛίʔυʹ͢Δͱܯࠂ͕ग़Δ ʮ0QUJPOBM͡Όͳ͍஋ͱOJMΛ  ɹൺֱͯ͠΋ৗʹGBMTFͩΑʯ 

30. ### enum Example { case zero case one(key: String) case two(key1:

String, key2: String) } 4XJGUͰ͸FOVNͷDBTFʹ෇ଐ͢Δ஋ΛఆٛͰ͖·͢ ͜ͷ෇ଐ͢Δ஋͸BTTPDJBUFEWBMVFTͱݺ͹Ε͍ͯ·͢
31. ### enum Example { case one(key: String) case two(key1: String, key2:

String) } func test() { let x1: Example = .one(key: "value") let x2: Example = .two(key1: "value1", key2: "value2") let mirror1 = Mirror(reflecting: x1) let mirror2 = Mirror(reflecting: x2) dump(mirror1.children.first!.value) dump(mirror2.children.first!.value) } ͦΕͧΕΛ.JSSPSʹ͔͚·͢ ͜͜Ͱ͸BTTPDJBUFEWBMVFTͷ  ཁૉ਺͕ҟͳΔͭͷDBTFΛ  ఆٛ͠·͢ BTTPDJBUFWBMVFT͸ɺ  TUSVDUͷϓϩύςΟͱ  ಉ͡Α͏ʹDIJMESFOͰ  දݱ͞Ε·͢ ͦΕͧΕͷDIJMESFOΛ  ֬ೝͯ͠Έ·͠ΐ͏
32. ### enum Example { case one(key: String) case two(key1: String, key2:

String) } func test() { let x1: Example = .one(key: "value") let x2: Example = .two(key1: "value1", key2: "value2") let mirror1 = Mirror(reflecting: x1) let mirror2 = Mirror(reflecting: x2) dump(mirror1.children.first!.value) dump(mirror2.children.first!.value) } BTTPDJBUFEWBMVFT͕  ͭͷ৔߹ WBMVF ͭͷ৔߹     LFZWBMVF   LFZWBMVF   ܕ͕ҧ͏ 

34. ### let y: Any = (number: 123) print(type(of: y)) ܕΛௐ΂ͯΈΔͱ  

*OU ຊ౰͸BTTPDJBUFEWBMVFTͷཁૉ਺͕  ͍ͭ͘Ͱ͋ΕɺDIJMESFOͷܕ͸UVQMFʹ  ͔ͨͬͨ͠ͷͰ͸ͱਪଌ ͔͠͠ɺཁૉ਺ͷ͚࣌ͩࣄ৘͕͋ͬͯ  UVQMFʹͰ͖ͳ͔͔ͬͨ΋͠Εͳ͍ͷͰɺ  ࢼ͠ʹཁૉ͕ͭͷUVQMFΛ࡞੒ͯ͠ΈΔ UVQMFͰ͸ͳ͍ 
35. ### // Error: cannot create a single-element tuple with // an

element label let x: (number: Int) = (number: 123)    let y: Any = (number: 123) print(type(of: y)) // Int ࣮͸ɺ4XJGUͷܕͰ͸ཁૉ͕ͭͷUVQMF͸ఆٛͰ͖ͳ͍ ͔͠͠ɺཁૉ਺ͷUVQMFܕ͕  ࡞Εͳ͍͚ͩͳͷͰɺܕΛ  "OZʹͯ͠͠·͑͹ཁૉ਺ͷ  UVQMFΒ͖͠΋ͷΛఆٛͰ͖Δ ͔͠͠ɺఆٛͰ͖ͨͱͯ͠΋ɺ  ͦͷܕ͸ཁૉͷத਎ͷܕʹ  ͳΔΑ͏ͩ ͭ·Γɺཁૉ਺ͷUVQMF͸  ଘࡏͰ͖ͳ͍͜ͱ͕Θ͔Δ