$30 off During Our Annual Pro Sale. View Details »

5分でわかるSE-0116(id-as-any)

takasek
October 26, 2016

 5分でわかるSE-0116(id-as-any)

potatotips #34 (http://potatotips.connpass.com/event/41363/) での発表資料です。

SE-0116 (https://github.com/apple/swift-evolution/blob/master/proposals/0116-id-as-any.md) のMotivationの章を重点的に解説。あと、as AnyObjectによるboxingでの注意点についても少し話しました。

takasek

October 26, 2016
Tweet

More Decks by takasek

Other Decks in Programming

Transcript

  1. 5෼ͰΘ͔Δ SE-0116(id-as-any) by takasek 2016/10/26 potatotips 34 1

  2. 2

  3. 3

  4. Swift Evolution Proposals SE-0116 Import Objective-C id as Swift Any

    type https://github.com/apple/swift-evolution/ blob/master/proposals/0116-id-as-any.md 4
  5. Proposal಺༰ Obj-Cͷidܕ͸ɺSwiftͰ͸ 4 ࠓ·ͰAnyObjectܕͱͯ͠දݱ͞Εͯͨ 4 ͕ɺAnyܕͱͯ͠දݱ͞ΕΔΑ͏ʹͳͬͨ 5

  6. 6

  7. Proposalͷಈػ 7

  8. Swift͸ ஋ܕਪ͠ 8

  9. ࢀরܕͬͯ ͔ͤͬ͘immutableʹͯ͠΋ ࢀরઌͷ಺༰͕มԽͯ͠ ࣄނΔ͜ͱ͕͋Δ 9

  10. ා͍ 10

  11. ʮOKɺ஋ܕΛ༻ҙ͠Α͏ʯ 4 String 4 Array 4 Dictionay 4 etc 11

  12. ! 12

  13. Ұํɺ Obj-C͸ id ܕʹΑͬͯଟଶੑΛ࣮ݱͯ͠Δ 4 id ܕ͸લఏͱͯ͠ΦϒδΣΫτܕ 4 Swiftͷ஋ܕ͸ΦϒδΣΫτܕ͡Όͳ͍ 13

  14. ͳͷͰ͜Ε·Ͱɺ ϒϦοδՄೳͳܕ͸ɺ ҉໧తʹΦϒδΣΫτܕʹม׵ 4 [AnyObject] ΛNSArrayͱͯ͠ѻ͏ 4 is, as?, as!ʹΑͬͯ஋ܕʹ໭ͤΔ

    14
  15. ศར 15

  16. ศར͚ͩͲɺ ༧ظ͠ͳ͍ಈ͖Λ ى͜͢͜ͱ΋ 16

  17. ࠓͷྲྀΕ ʮϠΫβ͗͢Δ͜ͱ͸ࢭΊΑ͏…ʯ SE-0072: ɹ໌ࣔతʹasͨ͠৔߹ʹ͔͠ ɹม׵Ͱ͖ͳ͍Α͏ʹ͠Α͏ʢ࣮૷ࡁʣ SE-0083: ɹasͷػೳ͸ܕνΣοΫ͚ͩʹͯ͠ɺ ɹಈతΩϟετ͸Ͱ͖ͳ͍Α͏ʹ͠Α͏ʢະ࣮૷ʣ 17

  18. ͦ͏͜͏ͯͨ͠Β Swift3Ͱ͸ Foundation͕ ஋ܕΛશ໘తʹ࠾༻ 18

  19. ʮDarwin(OS X)΋ଞϓϥοτϑΥʔϜ΋ ɹมΘΒͳ͍ΠϯλϑΣʔεΛʂʯ 19

  20. ʮAnyObject(Obj-Cͷ౎߹)ʹ ɹҾ͖ͣΒΕͯΔ৔߹͡Όͳ͍ʯ 20

  21. ϒϦοδՄೳͳܕͳΒ ΦϒδΣΫτܕʹม׵ʁ 21

  22. ؁͍ 22

  23. ͢΂ͯͷ஋ܕΛ ΦϒδΣΫτܕʹ ม׵Ͱ͖ΔΑ͏ʹʂ 23

  24. ͦ͏͢Ε͹ Foundation͔Β AnyObject (ΦϒδΣΫτܕ΁ͷґଘ)Λ ഉআͰ͖Δʂ 24

  25. ͱ͍͏Θ͚Ͱ 25

  26. Swiftͷ஋ܕ → Obj-Cͷidܕ 4 universal bridging conversionΛ৽ن࣮૷ Obj-Cͷidܕ → Swiftͷ஋ܕ

    4 طଘͷambivalent dynamic castingΛ࢖͏ 4 ͭ·Γ is , as? , as! AnyHashableϓϩτίϧΛ৽ن࣮૷ 4 Dictionary΍SetͷΩʔͰ࢖͏༻ɻ 4 ΦϒδΣΫτܕͳΒඞͣHashable͚ͩͬͨͲɺ ɹ ஋ܕ͕Hashableͱ͸ݶΒͳ͍ͷͰɻ 26
  27. ৄࡉઃܭ 27

  28. Any→AnyObjectͷϒϦοδ͸ɺ classes 4 ࢀরܕͳͷͰɺͦͷ·Μ·࢖͑Δ Bridged value types (String, Array, Dictionary,

    Set, etcͷ͜ͱ) 4 طଘͷ _ObjectiveCBridgeable ϓϩτίϧΛྲྀ༻ 4 SE-0116Ͱ͸ϊʔλον 4 ʢ͚Ͳɺকདྷతʹ͸΋ͬͱଟ͘ͷܕΛbridgeableʹ͍ͨ͠ʣ ͦͷଞͷ஋ܕ 4 immutableͳclassͰboxing͢Δ 4 ݩͷ஋ܕʹ໭ͤΔͳΒɺ಺෦࣮૷Λ֎ʹݟͤΔඞཁ͸ͳ͍ 28
  29. ஫ҙ఺ !AnyObjectܕ͕ഇࢭ͞Εͨ !Anyܕ͕ΦϒδΣΫτܕʹͳͬͨ 29

  30. ஫ҙ఺ !AnyObjectܕ͕ഇࢭ͞Εͨ !Anyܕ͕ΦϒδΣΫτܕʹͳͬͨ !AnyܕˠAnyObjectܕ΁ͷ ɹbridge͕׬උ͞Εͨ 30

  31. ஋ܕ͸ɺ as Any Ͱ͸ͳ͘ as AnyObject ͷλΠϛϯάͰboxing͞ΕΔ let value =

    1 as Any let a = value let b = value a as AnyObject === b as AnyObject //false as AnyObject ͷλΠϛϯά͕ҟͳΕ͹ɺ ผͷΦϒδΣΫτͱͯ͠boxing͞ΕΔ 31
  32. ΦϒδΣΫτܕʹͳͬͨޙ͸ɺ as Any ͯ͠΋box͕ҡ࣋͞ΕΔ let value = 1 as AnyObject

    let a = value as Any let b = value as Any a as AnyObject === b as AnyObject //true 32
  33. ͱ͍͏Θ͚Ͱ… ΦϒδΣΫτͷಉҰੑ൑ఆ͕ඞཁͳίʔυΛॻ͘ ͱ͖ʹ͸ؾΛ͚ͭΑ͏ʂ FoundationͷAPIͰ͢Βɺ AnyΛҾ਺ʹड͚͓͖ͯͳ͕Βɺ஋ܕΛ౉͢ͱόάΔ΋ͷ͕͋Γ·͢… (Xcode8.0ݱࡏ) 33

  34. 34

  35. Happy Swifting! 35