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

Blending Kotlin's culture into Swift

Yuka Ezura
August 09, 2017

Blending Kotlin's culture into Swift

Yuka Ezura

August 09, 2017
Tweet

More Decks by Yuka Ezura

Other Decks in Programming

Transcript

  1. when (instance as Super) { is SubA -> … //

    do something is SubB -> … // do something else -> … // fatal error? makeshift code? }.let { … } !!: Only when treating "when" as an expression, checked whether it covers all. switch-case
 expression noise… Kotlin
  2. switch instance as Super { case let a as SubA:

    … // do something case let b as SubB: … // do something default: … // fatal error? makeshift code? } noise… Even if the premise is broken, 
 we can not detect statically. Swift Inhibit the representation 
 that it covers the whole type in the context :(
  3. sad code… func didSelect(item: Item) { switch item { case

    let item as Book: // do something // use item as Book case let item as Memo: // do something // use item as Memo default: assertionFailure("unexpected type: \(item)") } } noise…
  4. when (instance as Super) { is SubA -> … //

    do something is SubB -> … // do something else -> … // fatal error? makeshift code? }.let { … } !!: Only when treating "when" as an expression, checked whether it covers all. switch-case
 expression Kotlin
  5. when (instance as Super) { is Super.SubA -> … //

    do something is Super.SubB -> … // do something else -> … // fatal error? makeshift code? }.let { … } noise… Kotlin !!: Only when treating "when" as an expression, checked whether it covers all.
  6. sealed class Super { // ... class SubA: Super() {

    // ... } class SubB: Super() { // ... } } Kotlin
  7. when (instance as Super) { is Super.SubA -> … //

    do something is Super.SubB -> … // do something } Kotlin
  8. Goal not for:
 Completely reproducing the function of `sealed` for:


    reproducing the concept of `sealed` To clarify that the code covers the types 
 that can be taken in the context Swift
  9. resolve this issue func didSelect(item: Item) { switch item {

    case let item as Book: // do something // use item as Book case let item as Memo: // do something // use item as Memo default: assertionFailure("unexpected type: \(item)") } } noise…
  10. protocol Item { // ... } struct Book: Item {

    // ... } struct Memo: Item { // ... } Swift
  11. func didSelect(item: ItemType) { switch item { case .book(let book):

    // do something // use item as Book case .memo(let memo): // do something // use item as Memo default: assertionFailure("unexpected type: \(item)") } } Swift noise…
  12. protocol Item { // ... var name: String { get

    } } struct Book: Item { // ... let name: String } struct Memo: Item { // ... let name: String } Swift
  13. let item: ItemType = … let name = item.name Swift

    error… enum ItemType { case book(Book) case memo(Memo) }
  14. let name: String = { switch item { case .book(let

    book): return book.name case .memo(let memo): return memo.name } }() Swift
  15. enum ItemType { case book(Book) case memo(Memo) } Swift Bad

    point: There is no (or weak) expression 
 that `enum ItemType` is a set of Sub types of “Item"
  16. enum ItemType { // … private var base: Item {

    switch self { case .book(let item as Item),
 .memo(let item as Item): return item } } var name: String { return base.name } } Swift 3
  17. enum ItemType: Item { // … private var base: Item

    { switch self { case .book(let item as Item),
 .memo(let item as Item): return item } } var name: String { return base.name } } Swift 3 or
  18. enum ItemType: Item { // ... private var base: Item

    { switch self { case .book(let item): return item case .memo(let item): return item } } var name: String { return base.name } } Xcode 9.0 beta 4
  19. enum ItemType { case book(Book) case memo(Memo) private var base:

    Item { switch self { case .book(let item as Item),
 .memo(let item as Item): return item } } var name: String { return base.name } } Swift result
  20. func didSelect(item: ItemType) { switch item { case .book(let book):

    // do something // use item as Book case .memo(let memo): // do something // use item as Memo default: assertionFailure("unexpected type: \(item)") } } Swift noise…
  21. Swift protocol Item { // ... var name: String {

    get } } let item: ItemType = … let name = item.name
  22. class SampleController { var item: Item // … func f()

    { switch item { case let item as Book: // do something // use item as Book case let item as Memo: // do something // use item as Memo default: assertionFailure("unexpected type: \(item)") } } } Swift before
  23. class SampleController { enum ItemType { case book(Book) case memo(Memo)

    private var base: Item { … } // … } var item: ItemType func f() { … } } // usage SampleController(item: .book(book)) SampleController(item: .memo(memo)) Swift after
  24. Conclusion blending Kotlin's “sealed class concept” into Swift We can

    express the context clearly with the above concept We can partially try this