Kotlin For Swift Developers - Swift Paris, October 2018

Kotlin For Swift Developers - Swift Paris, October 2018

Kotlin is a new language born on the JVM that's gained a ton of popularity with Android developers. Learn about its similarities and differences with Swift, and some new, crazy things you can do with Kotlin/Native (and some totally sane things you can't do).

Sandbox app mentioned in the talk: https://github.com/designatednerd/KotlinNativeTest

C4861b1dfdf3bbb21faec4a1acdf183d?s=128

Ellen Shapiro

October 24, 2018
Tweet

Transcript

  1. 1.

    KOTLIN FOR SWIFT DEVELOPERS SWIFT PARIS | PARIS, FRANCE |

    OCTOBER 2018 @DESIGNATEDNERD | BAKKENBAECK.COM | JUSTHUM.COM
  2. 2.

    KOTLIN POUR SWIFT DEVELOPPEURS SWIFT PARIS ! PARIS, FRANCE OCTOBRE

    2018 @DESIGNATEDNERD ! BAKKENBAECK.COM JUSTHUM.COM
  3. 3.

    !"

  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
  13. 14.
  14. 15.
  15. 16.
  16. 17.
  17. 19.
  18. 20.
  19. 28.

    !"

  20. 30.

    SWIFT func useFunction<T, U>(on item: T, function: (T) -> U)

    -> U { return function(item) } func insertExclamationPoint(in string: String) -> String { return string .components(separatedBy: " ") .joined(separator: "! ") } let result = useFunction(on: "try Swift", function: insertExclamationPoint(in:)) // result: "try! Swift"
  21. 31.

    KOTLIN fun <T, U>T.useFunction(action: (T) -> U): U { return

    action(this) } fun insertExclamationPoint(in: String) : String { return in.split(" ").joinToString("! ") } val result = "try Kotlin".useFunction { insertExclamationPoint(it) } // result: "try! Kotlin"
  22. 34.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(0, +) // reduced: 6
  23. 35.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(0, +) // // reduced: 6
  24. 36.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(0) { $0 + $1 } // reduced: 6
  25. 37.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26
  26. 38.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26 KOTLIN val numbers = arrayOf(1, 2, 3) val reduced = numbers.reduce { total, current -> total + current } // reduced: 6
  27. 39.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26 ⬆ KOTLIN val numbers = arrayOf(1, 2, 3) val reduced = numbers.reduce { total, current -> total + current } // reduced: 6
  28. 40.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26 KOTLIN val numbers = arrayOf(1, 2, 3) val folded = numbers.fold(20) ⬅ { total, current -> total + current } // folded: 26
  29. 42.

    SWIFT let doubled = [ 1, 2, 3 ].map {

    $0 * 2 } KOTLIN arrayOf(1, 2, 3).map { it * 2 }
  30. 43.
  31. 44.
  32. 45.

    SWIFT let doubled = [ 1, 2, 3 ].map {

    $0 * 2 } KOTLIN arrayOf(1, 2, 3).map { it * 2 }
  33. 46.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26
  34. 48.

    SWIFT let numbers = [ 1, 2, 3 ] let

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26 KOTLIN val numbers = arrayOf(1, 2, 3) val folded = numbers.fold(20) { total, current -> total + current } // folded: 26 ⬆
  35. 49.
  36. 50.

    !"#

  37. 51.
  38. 52.
  39. 59.
  40. 64.
  41. 66.

    PROPOSED (KOTLIN) interface SomeInterface { fun doAThing() } @DefaultImpl SomeInterface

    { fun doAThing() { println("Hi, I'm the default implementation!") } }
  42. 69.

    KOTLIN sealed class ViewState { class Empty(val placeholder: View): ViewState()

    class HasContent(val items: List<Item>): ViewState() }
  43. 70.

    KOTLIN sealed class ViewState { class Empty(val placeholder: View): ViewState()

    class HasContent(val items: List<Item>): ViewState() } when (state) { is ViewState.Empty --> display(it.placeholder) is ViewState.HasContent --> adapter.reloadWith(it.items) }
  44. 71.

    SWIFT enum TableViewState { case empty(placeholderView: UIView) case loaded(items: [Item])

    } switch state { case .empty(let placeholder): display(placeholder) case .hasContent(let items): dataSource.reloadWith(items) }
  45. 72.

    KOTLIN sealed class ViewState { class Empty(val placeholder: View): ViewState()

    class HasContent(val items: List<Item>): ViewState() }
  46. 73.

    KOTLIN sealed class ViewState { class Empty(val placeholder: View): ViewState()

    class HasContent(val items: List<Item>): ViewState() { val adapter = RecyclerViewAdapter(items) fun selectedItem(item: Item) { // Do something ith the selected item } } }
  47. 74.

  48. 75.
  49. 76.
  50. 78.
  51. 80.
  52. 81.
  53. 82.
  54. 83.

    SUPPORTED PLATFORMS ▸ x86_64 (macOS, iOS Sim, Linux, Windows) ▸

    arm64 (iOS, Android) ▸ arm32 (iOS, Android, Linux) ▸ arm32 hardfp (Raspberry Pi) ▸ MIPS (Linux) ▸ wasm32 (Web Assembly)
  55. 84.
  56. 85.
  57. 86.
  58. 87.
  59. 89.
  60. 90.
  61. 91.
  62. 93.
  63. 94.
  64. 95.
  65. 97.
  66. 98.

    KOTLIN / NATIVE val decoded = NSString.create( data = data,

    encoding = NSUTF8StringEncoding ) // `decoded` type: NSString
  67. 99.
  68. 100.
  69. 101.
  70. 102.
  71. 103.
  72. 104.
  73. 106.
  74. 107.
  75. 109.

    !

  76. 110.

    + =

  77. 112.
  78. 115.
  79. 116.
  80. 119.

    SWIFT: ENUMS enum Things { case monday case tuesday case

    wednesday case thursday case friday case saturday case sunday }
  81. 121.
  82. 125.

    SWIFT SWITCHING ON A KOTLIN ENUM switch day { case

    .monday: print("You can fall apart") case .tuesday, .wednesday: print("Break my heart") case .thursday: print("Doesn't even start") case .friday: print("I'm in love") case .saturday: print("Wait") case .sunday: print("Always comes too late") } // ! Seems legit...
  83. 126.

    SWIFT SWITCHING ON A KOTLIN ENUM switch day { case

    .monday: print("You can fall apart") case .tuesday, .wednesday: print("Break my heart") case .thursday: print("Doesn't even start") case .friday: print("I'm in love") case .saturday: print("Wait") case .sunday: print("Always comes too late") } // ! ERROR: ! Switch must be exhaustive
  84. 127.

    SWIFT SWITCHING ON A KOTLIN ENUM switch day { case

    .monday: print("You can fall apart") case .tuesday, .wednesday: print("Break my heart") case .thursday: print("Doesn't even start") case .friday: print("I'm in love") case .saturday: print("Wait") case .sunday: print("Always comes too late") default: fatalError("Not a day") // ! }
  85. 130.
  86. 135.
  87. 136.
  88. 138.
  89. 139.
  90. 142.

    OBLIGATORY SUMMARY SLIDE ▸ Literally put the fun in functional

    programming ▸ Make Friends With Android
  91. 143.

    OBLIGATORY SUMMARY SLIDE ▸ Literally put the fun in functional

    programming ▸ Make Friends With Android ▸ Kotlin Native Could Be A Whole New World
  92. 144.

    OBLIGATORY SUMMARY SLIDE ▸ Literally put the fun in functional

    programming ▸ Make Friends With Android ▸ Kotlin Native Could Be A Whole New World ▸ try! Kotlin
  93. 147.
  94. 148.
  95. 149.

    LINKS! ▸ Kotlin Home & Documentation: https://kotlinlang.org ▸ Kotlin Native

    MultiplatformApp tutorial https://kotlinlang.org/docs/tutorials/ native/mpp-ios-android.html ▸ KotlinConf iOS App in Kotlin/Native: https://github.com/JetBrains/kotlinconf- app/tree/master/konfios
  96. 150.

    MORE LINKS! ▸ The Edu Tools Plugin for IntelliJ or

    Android Studio https://kotlinlang.org/docs/tutorials/ edu-tools-learner.html ▸ Representing State (KotlinConf talk on Sealed Classes) https://www.youtube.com/watch?v=- lVVfxsRjcY ▸ Sealed Classes Opened My Mind (KotlinConf Talk) https://www.youtube.com/watch? v=uGMm3StjqLI
  97. 151.

    SHAMELESS SELF-PROMOTION! ▸ My talk on Protocol-Oriented Programming from Dot

    Swift https://www.dotconferences.com/2018/01/ ellen-shapiro-protocols-all-the-way-down ▸ Kotlin Apprentice https://store.raywenderlich.com/products/ kotlin-apprentice ▸ Android Apprentice https://store.raywenderlich.com/products/ android-apprentice
  98. 152.