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

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

Ellen Shapiro

October 24, 2018
Tweet

More Decks by Ellen Shapiro

Other Decks in Technology

Transcript

  1. KOTLIN
    FOR SWIFT DEVELOPERS
    SWIFT PARIS | PARIS, FRANCE | OCTOBER 2018
    @DESIGNATEDNERD | BAKKENBAECK.COM | JUSTHUM.COM

    View full-size slide

  2. KOTLIN
    POUR SWIFT DEVELOPPEURS
    SWIFT PARIS
    !
    PARIS, FRANCE OCTOBRE 2018
    @DESIGNATEDNERD
    !
    BAKKENBAECK.COM JUSTHUM.COM

    View full-size slide

  3. WHAT IS KOTLIN ?

    View full-size slide

  4. WHY SHOULD I CARE?

    View full-size slide

  5. A CONSTANT STRING

    View full-size slide

  6. SWIFT
    let greeting = "Hello World"

    View full-size slide

  7. SWIFT
    let greeting = "Hello World"
    KOTLIN
    val greeting = "Hello World"

    View full-size slide

  8. AN OPTIONAL INTEGER VARIABLE

    View full-size slide

  9. SWIFT
    var something: Int?

    View full-size slide

  10. SWIFT
    var something: Int?
    KOTLIN
    var something: Int?

    View full-size slide


  11. NullPointerException

    View full-size slide

  12. GENERICS
    AND
    FUNCTIONS AS PARAMETERS

    View full-size slide

  13. SWIFT
    func useFunction(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"

    View full-size slide

  14. KOTLIN
    fun 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"

    View full-size slide

  15. map
    filter
    reduce

    View full-size slide

  16. map
    filter
    reduce*

    View full-size slide

  17. SWIFT
    let numbers = [ 1, 2, 3 ]
    let reduced = numbers.reduce(0, +)
    // reduced: 6

    View full-size slide

  18. SWIFT
    let numbers = [ 1, 2, 3 ]
    let reduced = numbers.reduce(0, +) //
    // reduced: 6

    View full-size slide

  19. SWIFT
    let numbers = [ 1, 2, 3 ]
    let reduced = numbers.reduce(0) { $0 + $1 }
    // reduced: 6

    View full-size slide

  20. SWIFT
    let numbers = [ 1, 2, 3 ]
    let reduced = numbers.reduce(20) { $0 + $1 }
    // reduced: 26

    View full-size slide

  21. 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

    View full-size slide

  22. 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

    View full-size slide

  23. 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

    View full-size slide

  24. SWIFT
    let doubled = [ 1, 2, 3 ].map { $0 * 2 }

    View full-size slide

  25. SWIFT
    let doubled = [ 1, 2, 3 ].map { $0 * 2 }
    KOTLIN
    arrayOf(1, 2, 3).map { it * 2 }

    View full-size slide

  26. SWIFT
    let doubled = [ 1, 2, 3 ].map { $0 * 2 }
    KOTLIN
    arrayOf(1, 2, 3).map { it * 2 }

    View full-size slide

  27. SWIFT
    let numbers = [ 1, 2, 3 ]
    let reduced = numbers.reduce(20) { $0 + $1 }
    // reduced: 26

    View full-size slide

  28. $0 $1 $2...$∞

    View full-size slide

  29. 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

    View full-size slide

  30. NOW (KOTLIN)
    optionalThing?.let {
    it.doSomething()
    }

    View full-size slide

  31. NOW (KOTLIN)
    optionalThing?.let {
    it.doSomething()
    } else {
    doSomethingElse()
    }

    View full-size slide

  32. NOW (KOTLIN)
    optionalThing?.let {
    it.doSomething()
    } else { //
    !
    Not possible
    doSomethingElse()
    }

    View full-size slide

  33. NOW (KOTLIN)
    if (optionalThing != nil) {
    optionalThing.doSomething()
    } else {
    doSomethingElse()
    }

    View full-size slide

  34. NOW (KOTLIN)
    if (optionalThing != nil) {
    optionalThing.doSomething() //<- magic unwrap!!
    } else {
    doSomethingElse()
    }

    View full-size slide

  35. PROPOSED (KOTLIN)
    guard val thing = optionalThing else {
    return
    }
    thing.doSomething()

    View full-size slide

  36. NOW (SWIFT)
    UserDefaults.standard.set(true, forKey: "DefaultOne")
    UserDefaults.standard.set(false, forKey: "DefaultTwo)

    View full-size slide

  37. NOW (BETTER SWIFT)
    let defaults = UserDefaults.standard
    defaults.set(true, forKey: "DefaultOne")
    defaults.set(false, forKey: "DefaultTwo)

    View full-size slide

  38. PROPOSED (SWIFT)
    UserDefaults.standard.apply {
    "DefaultOne" = true,
    "DefaultTwo" = false,
    }

    View full-size slide

  39. PROTOCOLS WITH
    DEFAULT IMPLEMENTATIONS

    View full-size slide

  40. PROPOSED (KOTLIN)
    interface SomeInterface {
    fun doAThing()
    }

    View full-size slide

  41. PROPOSED (KOTLIN)
    interface SomeInterface {
    fun doAThing()
    }
    @DefaultImpl
    SomeInterface {
    fun doAThing() {
    println("Hi, I'm the default implementation!")
    }
    }

    View full-size slide

  42. @JVMDefault + DefaultImpl
    (SORT OF)

    View full-size slide

  43. SEALED CLASSES

    View full-size slide

  44. KOTLIN
    sealed class ViewState {
    class Empty(val placeholder: View): ViewState()
    class HasContent(val items: List): ViewState()
    }

    View full-size slide

  45. KOTLIN
    sealed class ViewState {
    class Empty(val placeholder: View): ViewState()
    class HasContent(val items: List): ViewState()
    }
    when (state) {
    is ViewState.Empty --> display(it.placeholder)
    is ViewState.HasContent --> adapter.reloadWith(it.items)
    }

    View full-size slide

  46. 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)
    }

    View full-size slide

  47. KOTLIN
    sealed class ViewState {
    class Empty(val placeholder: View): ViewState()
    class HasContent(val items: List): ViewState()
    }

    View full-size slide

  48. KOTLIN
    sealed class ViewState {
    class Empty(val placeholder: View): ViewState()
    class HasContent(val items: List): ViewState() {
    val adapter = RecyclerViewAdapter(items)
    fun selectedItem(item: Item) {
    // Do something ith the selected item
    }
    }
    }

    View full-size slide

  49. KOTLIN / NATIVE

    View full-size slide

  50. PLUS ÇA CHANGE

    View full-size slide

  51. 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)

    View full-size slide

  52. KOTLIN IOS LIBRARY
    !
    WRAPPERS

    View full-size slide

  53. KOTLIN IOS LIBRARY
    !
    WRAPPERS*
    *(WHICH USUALLY WORK)

    View full-size slide

  54. KOTLIN / NATIVE
    val decoded = NSString.create(
    data = data,
    encoding = NSUTF8StringEncoding
    )

    View full-size slide

  55. KOTLIN / NATIVE
    val decoded = NSString.create(
    data = data,
    encoding = NSUTF8StringEncoding
    )
    // `decoded` type: NSString

    View full-size slide

  56. BUSINESS LOGIC

    View full-size slide

  57. KOTLIN / NATIVE GOTCHAS

    View full-size slide

  58. MEMORY MANAGEMENT
    AND CONCURRENCY
    !!!!!!!

    View full-size slide

  59. COROUTINES*
    * ONLY WORK ON THE MAIN THREAD IN KOTLIN / NATIVE

    View full-size slide

  60. (˽s̕s҂˽Ɨ ˍʓˍ

    View full-size slide

  61. https://github.com/designatednerd/
    KotlinNativeTest

    View full-size slide

  62. OBJ-C INTEROP HILARITY

    View full-size slide

  63. SWIFT: ENUMS
    enum Things {
    case monday
    case tuesday
    case wednesday
    case thursday
    case friday
    case saturday
    case sunday
    }

    View full-size slide

  64. KOTLIN: ENUM CLASSES
    enum class DayOfWeek {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday;
    }

    View full-size slide

  65. A KOTLIN ENUM CLASS IN A KOTLIN / NATIVE FRAMEWORK

    View full-size slide

  66. A KOTLIN ENUM CLASS IN A KOTLIN / NATIVE FRAMEWORK

    View full-size slide

  67. A KOTLIN ENUM CLASS IN A KOTLIN / NATIVE FRAMEWORK

    View full-size slide

  68. 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...

    View full-size slide

  69. 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

    View full-size slide

  70. 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") //
    !
    }

    View full-size slide

  71. DOCUMENTATION

    View full-size slide

  72. DOCUMENTATION?
    ¯\_(ϑ)_/¯

    View full-size slide

  73. PRODUCTION?!

    View full-size slide

  74. KOTLIN:

    OR
    "
    =

    View full-size slide

  75. https://try.kotlinlang.org

    View full-size slide

  76. EDU TOOLS
    PLUGIN FOR INTELLIJ OR ANDROID STUDIO

    View full-size slide

  77. OBLIGATORY SUMMARY SLIDE

    View full-size slide

  78. OBLIGATORY SUMMARY SLIDE
    ▸ Literally put the fun in functional programming

    View full-size slide

  79. OBLIGATORY SUMMARY SLIDE
    ▸ Literally put the fun in functional programming
    ▸ Make Friends With Android

    View full-size slide

  80. OBLIGATORY SUMMARY SLIDE
    ▸ Literally put the fun in functional programming
    ▸ Make Friends With Android
    ▸ Kotlin Native Could Be A Whole New World

    View full-size slide

  81. OBLIGATORY SUMMARY SLIDE
    ▸ Literally put the fun in functional programming
    ▸ Make Friends With Android
    ▸ Kotlin Native Could Be A Whole New World
    ▸ try! Kotlin

    View full-size slide

  82. ONE MORE THING

    View full-size slide

  83. ONE MORE THING:
    (A SHAMELESS PLUG!)

    View full-size slide

  84. 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

    View full-size slide

  85. 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

    View full-size slide

  86. 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

    View full-size slide

  87. ILLUSTRATION CREDITS
    ▸ Monica Komperda, Native Code Forever
    (Hire her! She's awesome!)
    http://mokomperda.com/design

    View full-size slide