Pro Yearly is on sale from $80 to $50! »

SwiftのNeverとボトム型 / Never as a Bottom Type

473b12ebcfb79adfaef97796e94fc25c?s=47 Hiron
April 14, 2019

SwiftのNeverとボトム型 / Never as a Bottom Type

第7回カンファレンス動画鑑賞会で供養したLTの資料です。

473b12ebcfb79adfaef97796e94fc25c?s=128

Hiron

April 14, 2019
Tweet

Transcript

  1. 4XJGUͷNeverͱ
 ϘτϜܕ 2019/04/14 ୈ7ճΧϯϑΝϨϯεಈըؑ৆ձ Mobile Act OSAKA #9 (ิܽ) try!

    Swift Tokyo 2019 (Backup)
  2. About Me w ͻΖΜʢҰٶߒڭʣ w 5XJUUFS(JU)VC
 2JJUB4MJEF4IBSF 4QFBLFS%FDL
 ˠIJSPOZUJD w

    ಙౡࡏॅ
  3. ݱ࣌఺ͷ4XJGUͰ͸
 Never͸ϘτϜܕͰ͸͋Γ·ͤΜ

  4. Agenda w NeverܕͬͯԿ͚ͩͬʁ w ϘτϜܕͬͯԿΑʁ w ΋͠Never͕ϘτϜܕͩͬͨΒʁ

  5. Never Type

  6. func foo() -> Type foo͸TypeͷΠϯελϯεΛฦ͢

  7. func foo() -> Int foo͸IntͷΠϯελϯεΛฦ͢ -3, 0, 10, 42, …

  8. func foo() -> Void foo͸஋Λฦ͞ͳ͍

  9. func foo() -> Type foo͸TypeͷΠϯελϯεΛฦ͢

  10. func foo() -> Void foo͸VoidͷΠϯελϯεΛฦ͢

  11. Void Type w ۭͷλϓϧܕͷΤΠϦΞε w ۭͷλϓϧͷܕʜ() w ͦͷ།Ұͷ஋ʜ()

  12. None
  13. func foo() -> Never foo͸ฦͬͯ͜ͳ͍

  14. func foo() -> Type foo͸TypeͷΠϯελϯεΛฦ͢

  15. func foo() -> Never foo͸NeverͷΠϯελϯεΛฦ͢

  16. Never Type w DBTFΛ࣋ͨͳ͍FOVN FOVN/FWFS\^  w ΠϯελϯεΛ࣋ͨͳ͍ w ؔ਺͸/FWFSͷΠϯελϯεΛฦͤͳ͍

    w ແݶϧʔϓɺΫϥογϡɺϓϩάϥϜऴྃɺྫ֎
  17. Bottom Type

  18. Subtype class Animal { ... } class Dog: Animal {

    ... } Animal Dog
  19. Subtype Dog͸AnimalͷαϒλΠϓ DogͷΠϯελϯεΛ
 Animalͱͯ͠ѻ͑Δ Animal Dog

  20. Animal Dog Cat Animal Dog Cat

  21. Animal Dog Cat Plant Flower Star … ?

  22. Animal Dog Cat Plant Flower Star … Any Top Type

  23. Animal Dog Cat Plant Flower Star … Any Top Type

    ? Bottom Type
  24. Bottom Type w ϘτϜܕ͸͢΂ͯͷܕͷαϒλΠϓ w ϘτϜܕͷΠϯελϯεΛͲͷܕͱͯ͠΋ѻ͑Δ

  25. 4501ࡀະຬҿञ ະ੒೥ऀͷҿञ͸๏཯Ͱې͡ΒΕ͍ͯ·͢

  26. Bottom Type w ϘτϜܕ͸͢΂ͯͷܕͷαϒλΠϓ w ϘτϜܕͷΠϯελϯεΛͲͷܕͱͯ͠΋ѻ͑Δ w ϘτϜܕ͸ΠϯελϯεΛ࣋ͨͳ͍ܕ

  27. Imagine:
 Never as a Bottom Type

  28. Case 1 class FooViewController: UIViewController { var label: UILabel! ...

    }
  29. Case 1 class FooViewController: UIViewController { lazy var label: UILabel

    = fatalError() ... }
  30. Case 2 let data: [String: String] = ... guard let

    name = data["name"] else { throw ParseError.missing("name") } speaker.name = name guard let icon = data["icon"] else { throw ParseError.missing("icon") } speaker.icon = icon
  31. Case 2 let data: [String: String] = ... speaker.name =

    data["name"] ?? try missing("name") speaker.icon = data["icon"] ?? try missing("icon") func missing(_ key: String) throws -> Never { throw ParseError.missing(key) }
  32. Case 2+ let data: [String: String] = ... speaker.name =

    data["name"] ?? throw ParseError.missing("name") speaker.icon = data["icon"] ?? throw ParseError.missing("icon") ͞Βʹthrow͕NeverΛฦࣜ͢ͳΒ
  33. Case 3 enum Result<Value, Error: Swift.Error> { case success(Value) case

    failure(Error) } func foo() -> Result<String, Never> { ... } foo͸ࣦഊ͠ͳ͍
  34. Case 3 • 4XJGUͰ࣮ݱ͞Ε·ͨ͠ 4& https://github.com/apple/swift-evolution/blob/master/proposals/0215-conform-never-to- hashable-and-equatable.md

  35. Case 3 w 4&ͷٞ࿦ʹͯ https://forums.swift.org/t/se-0215-conform-never-to-equatable-and-hashable/13586

  36. Protocol • ΠϯελϯεϓϩύςΟ (var) • Πϯελϯεϝιου (func) • ܕϓϩύςΟ (static

    var) • ܕϝιου (static func) • ΠχγϟϥΠβ (init) Πϯελϯεʹର͢Δ
 ੍໿Ͱ͸ͳ͍
  37. Recap

  38. Recap w NeverܕͬͯԿ͚ͩͬʁ w ϘτϜܕͬͯԿΑʁ w ΋͠Never͕ϘτϜܕͩͬͨΒʁ
 ˠΠϯελϯεΛ࣋ͨͳ͍ܕ ˠ͢΂ͯͷܕͷαϒλΠϓʹͳΔܕ ˠ͍Ζ͍ΖεοΩϦ͢Δ͜ͱ͕͋Δ

  39. Never as a Bottom Type • SE-0215 - Conform Never

    to Equatable and Hashable
 https://forums.swift.org/t/se-0215-conform-never-to-equatable-and-hashable/13586 • SE-0102: Remove @noreturn attribute and introduce an empty Never type
 https://github.com/apple/swift-evolution/blob/master/proposals/0102-noreturn-bottom- type.md • [Pitch] Never as a bottom type
 https://forums.swift.org/t/pitch-never-as-a-bottom-type/5920
  40. One more thing: Kotlin 4XJGU Void Never ,PUMJO Unit Nothing

    Nothing͸ϘτϜܕ
  41. Case 2 (Kotlin) val data: Map<String, String> = ... speaker.name

    = data["name"] ?: throw MissingException("name") speaker.icon = data["icon"] ?: throw MissingException("icon") throw͸NothingΛฦࣜ͢
  42. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠