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

Kotlin For Swift Developers - Swift Paris, Octo...

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
  2. KOTLIN POUR SWIFT DEVELOPPEURS SWIFT PARIS ! PARIS, FRANCE OCTOBRE

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

  4. !"

  5. 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"
  6. 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"
  7. SWIFT let numbers = [ 1, 2, 3 ] let

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

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

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

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26
  11. 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
  12. 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
  13. 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
  14. SWIFT let doubled = [ 1, 2, 3 ].map {

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

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

    reduced = numbers.reduce(20) { $0 + $1 } // reduced: 26
  17. 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 ⬆
  18. !"#

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

    { fun doAThing() { println("Hi, I'm the default implementation!") } }
  20. KOTLIN sealed class ViewState { class Empty(val placeholder: View): ViewState()

    class HasContent(val items: List<Item>): ViewState() }
  21. 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) }
  22. 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) }
  23. KOTLIN sealed class ViewState { class Empty(val placeholder: View): ViewState()

    class HasContent(val items: List<Item>): ViewState() }
  24. 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 } } }
  25. 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)
  26. KOTLIN / NATIVE val decoded = NSString.create( data = data,

    encoding = NSUTF8StringEncoding ) // `decoded` type: NSString
  27. !

  28. + =

  29. SWIFT: ENUMS enum Things { case monday case tuesday case

    wednesday case thursday case friday case saturday case sunday }
  30. 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...
  31. 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
  32. 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") // ! }
  33. OBLIGATORY SUMMARY SLIDE ▸ Literally put the fun in functional

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

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

    programming ▸ Make Friends With Android ▸ Kotlin Native Could Be A Whole New World ▸ try! Kotlin
  36. 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
  37. 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
  38. 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