my transition
play

MY TRANSITION FROM SWIFT TO KOTLIN Twitter: @allonsykraken Blog: - PowerPoint PPT Presentation

MY TRANSITION FROM SWIFT TO KOTLIN Twitter: @allonsykraken Blog: krakendev.io A LITTLE BIT ABOUT ME Twitter: @allonsykraken Blog: krakendev.io > Write and maintain a blog at krakendev.io that gets close to 10K views a week > Spoken about


  1. MY TRANSITION FROM SWIFT TO KOTLIN Twitter: @allonsykraken Blog: krakendev.io

  2. A LITTLE BIT ABOUT ME Twitter: @allonsykraken Blog: krakendev.io

  3. > Write and maintain a blog at krakendev.io that gets close to 10K views a week > Spoken about iOS/Swift in 3 different continents > Sr. iOS Developer for almost 6 years now > I work at Twitter (tweet, tweet) Twitter: @allonsykraken Blog: krakendev.io

  4. I WANTED MORE Twitter: @allonsykraken Blog: krakendev.io

  5. SO I LOOKED AND I SEARCHED Twitter: @allonsykraken Blog: krakendev.io

  6. AND ABOUT A YEAR AGO Twitter: @allonsykraken Blog: krakendev.io

  7. I FOUND IT Twitter: @allonsykraken Blog: krakendev.io

  8. BUT WE NEED YOU FOR THIS PROJECT Twitter: @allonsykraken Blog: krakendev.io

  9. Twitter: @allonsykraken Blog: krakendev.io

  10. LET'S PLAY A GAME Twitter: @allonsykraken Blog: krakendev.io

  11. SWIFT: 0 KOTLIN: 0 Twitter: @allonsykraken Blog: krakendev.io

  12. DATA CLASSES Twitter: @allonsykraken Blog: krakendev.io

  13. data class Conference(val name: String, val date: Date, val speakers: List<Speaker>) val kotlinConf = Conference(name = "KotlinConf", date = Date(), speakers = listOf()) Twitter: @allonsykraken Blog: krakendev.io

  14. struct Conference { let name: String let date: Date let speakers: [Speaker] } let kotlinConf = Conference(name: "KotlinConf", date: Date(), speakers: []) Twitter: @allonsykraken Blog: krakendev.io

  15. struct Conference { let name: String = "KotlinConf" let date: Date let speakers: [Speaker] } // This line throws a compiler error ! let kotlinConf = Conference(name: "BootlegKotlinConf", date: Date(), speakers: []) // We have to initialize without the default value. ✅ let kotlinConf = Conference(date: Date(), speakers: []) Twitter: @allonsykraken Blog: krakendev.io

  16. SWIFT: 0 KOTLIN: 1 Twitter: @allonsykraken Blog: krakendev.io

  17. ENUMS Twitter: @allonsykraken Blog: krakendev.io

  18. KOTLIN enum class ColorSpace { CYMK, RGBA, HSBA } // LOVE this feature ❤ val allColorSpaces = ColorSpace.allValues() for (colorSpace in allColorSpaces) { print(colorSpace.name) print(colorSpace.ordinal) } Twitter: @allonsykraken Blog: krakendev.io

  19. SWIFT enum ColorSpace { // Love this feature more though ❤ case cymk(Float, Float, Float, Float), case rgb(Float, Float, Float), case hsb(Float, Float, Float) } switch colorSpace { case .cymk(let cyan, let yellow, let magenta, let black): case .rgb(let red, let green, let blue): case .hsb(let hue, let saturation, let brightness): } Twitter: @allonsykraken Blog: krakendev.io

  20. EXAMPLE: EXPRESSIONS indirect enum Expr { case value(Double) case sum(Expr, Expr), var eval: Double { switch self { case .number(let value): return value case .sum(let lhs, let rhs): return lhs.eval + rhs.eval } } Twitter: @allonsykraken Blog: krakendev.io

  21. Twitter: @allonsykraken Blog: krakendev.io

  22. KOTLIN SEALED CLASSES sealed class Expr data class Const(val number: Double) : Expr() data class Sum(val e1: Expr, val e2: Expr) : Expr() fun eval(expr: Expr): Double = when(expr) { is Const -> expr.number is Sum -> eval(expr.e1) + eval(expr.e2) } Twitter: @allonsykraken Blog: krakendev.io

  23. SWIFT: 1 KOTLIN: 1 Twitter: @allonsykraken Blog: krakendev.io

  24. DELEGATED PROPERTIES & CLASSES Twitter: @allonsykraken Blog: krakendev.io

  25. EXAMPLE: VIEW ASSIGNMENT public class PersonView(context: Context) : LinearLayout(context) { val firstName: TextView by bindView(R.id.first_name) } Twitter: @allonsykraken Blog: krakendev.io

  26. SWIFT: 1 KOTLIN: 2 Twitter: @allonsykraken Blog: krakendev.io

  27. TUPLES Twitter: @allonsykraken Blog: krakendev.io

  28. let person = (name: "Paul", age: 35) func printPerson(person: (String, Int)) { print("\(person.0) is \(person.1) years old") } Twitter: @allonsykraken Blog: krakendev.io

  29. // Have to create an entire class // to destructure into two variables, not one. val (name, age) = Person() Twitter: @allonsykraken Blog: krakendev.io

  30. SWIFT: 2 KOTLIN: 2 Twitter: @allonsykraken Blog: krakendev.io

  31. EASE OF LEARNING Twitter: @allonsykraken Blog: krakendev.io

  32. SWIFT: 3 KOTLIN: 2 Twitter: @allonsykraken Blog: krakendev.io

  33. MEMORY MANAGEMENT Twitter: @allonsykraken Blog: krakendev.io

  34. SWIFT: 4 KOTLIN: 2 Twitter: @allonsykraken Blog: krakendev.io

  35. COROUTINES Twitter: @allonsykraken Blog: krakendev.io

  36. SWIFT: 4 KOTLIN: 3 Twitter: @allonsykraken Blog: krakendev.io

  37. TRUE TYPE ERASURE Twitter: @allonsykraken Blog: krakendev.io

  38. SWIFT protocol Pokemon { associatedType Type func attack() -> Type } KOTLIN interface Pokemon<out Type> { fun attack(): Type } Twitter: @allonsykraken Blog: krakendev.io

  39. class PokemonTrainer { var favoritePokemon: Pokemon // Won't compile } Twitter: @allonsykraken Blog: krakendev.io

  40. struct AnyPokemon<P: Pokemon>: Pokemon { private let pokemon: P init(_ pokemon: P) { self.pokemon = pokemon } func attack() -> P.Type { return pokemon.attack() } } class PokemonTrainer { var favoritePokemon: AnyPokemon<Fire> } // Usage trainer.favoritePokemon = AnyPokemon(Charmander()) Twitter: @allonsykraken Blog: krakendev.io

  41. GOOD OL' KOTLIN data class Trainer(val favoritePokemon: Pokemon<Any>) Twitter: @allonsykraken Blog: krakendev.io

  42. SWIFT: 4 KOTLIN: 4 Twitter: @allonsykraken Blog: krakendev.io

  43. INTEROPERABILITY Twitter: @allonsykraken Blog: krakendev.io

  44. SWIFT: 4 KOTLIN: 5 Twitter: @allonsykraken Blog: krakendev.io

  45. VALUE/REFERENCE SEMANTICS Twitter: @allonsykraken Blog: krakendev.io

  46. var developers = ["Hector", "Aaron", "Tre"] for developer in developers { if !developer.isCool() { developers.remove(developer) } } print(developers) // Prints out "Hector" Twitter: @allonsykraken Blog: krakendev.io

  47. // reference semantics class Person { let name: String } // value semantics struct Person { let name: String } Twitter: @allonsykraken Blog: krakendev.io

  48. FLEXIBILITY/CONTROL Twitter: @allonsykraken Blog: krakendev.io

  49. SWIFT: 5 KOTLIN: 5 Twitter: @allonsykraken Blog: krakendev.io

  50. Twitter: @allonsykraken Blog: krakendev.io

  51. Twitter: @allonsykraken Blog: krakendev.io

  52. SWIFT: 5 KOTLIN: 6 Twitter: @allonsykraken Blog: krakendev.io

  53. !!! Twitter: @allonsykraken Blog: krakendev.io

  54. ! Twitter: @allonsykraken Blog: krakendev.io

  55. ❤ Twitter: @allonsykraken Blog: krakendev.io

Recommend


More recommend