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

my transition
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

MY TRANSITION

FROM SWIFT TO KOTLIN

Twitter: @allonsykraken Blog: krakendev.io
slide-2
SLIDE 2

A LITTLE BIT ABOUT ME

Twitter: @allonsykraken Blog: krakendev.io
slide-3
SLIDE 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
slide-4
SLIDE 4

I WANTED MORE

Twitter: @allonsykraken Blog: krakendev.io
slide-5
SLIDE 5

SO I LOOKED AND I SEARCHED

Twitter: @allonsykraken Blog: krakendev.io
slide-6
SLIDE 6

AND ABOUT A YEAR AGO

Twitter: @allonsykraken Blog: krakendev.io
slide-7
SLIDE 7

I FOUND IT

Twitter: @allonsykraken Blog: krakendev.io
slide-8
SLIDE 8

BUT WE NEED YOU FOR THIS PROJECT

Twitter: @allonsykraken Blog: krakendev.io
slide-9
SLIDE 9 Twitter: @allonsykraken Blog: krakendev.io
slide-10
SLIDE 10

LET'S PLAY A GAME

Twitter: @allonsykraken Blog: krakendev.io
slide-11
SLIDE 11

SWIFT: 0 KOTLIN: 0

Twitter: @allonsykraken Blog: krakendev.io
slide-12
SLIDE 12

DATA CLASSES

Twitter: @allonsykraken Blog: krakendev.io
slide-13
SLIDE 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
slide-14
SLIDE 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
slide-15
SLIDE 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
slide-16
SLIDE 16

SWIFT: 0 KOTLIN: 1

Twitter: @allonsykraken Blog: krakendev.io
slide-17
SLIDE 17

ENUMS

Twitter: @allonsykraken Blog: krakendev.io
slide-18
SLIDE 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
slide-19
SLIDE 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
slide-20
SLIDE 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
slide-21
SLIDE 21 Twitter: @allonsykraken Blog: krakendev.io
slide-22
SLIDE 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
slide-23
SLIDE 23

SWIFT: 1 KOTLIN: 1

Twitter: @allonsykraken Blog: krakendev.io
slide-24
SLIDE 24

DELEGATED PROPERTIES & CLASSES

Twitter: @allonsykraken Blog: krakendev.io
slide-25
SLIDE 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
slide-26
SLIDE 26

SWIFT: 1 KOTLIN: 2

Twitter: @allonsykraken Blog: krakendev.io
slide-27
SLIDE 27

TUPLES

Twitter: @allonsykraken Blog: krakendev.io
slide-28
SLIDE 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
slide-29
SLIDE 29

// Have to create an entire class // to destructure into two variables, not one. val (name, age) = Person()

Twitter: @allonsykraken Blog: krakendev.io
slide-30
SLIDE 30

SWIFT: 2 KOTLIN: 2

Twitter: @allonsykraken Blog: krakendev.io
slide-31
SLIDE 31

EASE OF LEARNING

Twitter: @allonsykraken Blog: krakendev.io
slide-32
SLIDE 32

SWIFT: 3 KOTLIN: 2

Twitter: @allonsykraken Blog: krakendev.io
slide-33
SLIDE 33

MEMORY MANAGEMENT

Twitter: @allonsykraken Blog: krakendev.io
slide-34
SLIDE 34

SWIFT: 4 KOTLIN: 2

Twitter: @allonsykraken Blog: krakendev.io
slide-35
SLIDE 35

COROUTINES

Twitter: @allonsykraken Blog: krakendev.io
slide-36
SLIDE 36

SWIFT: 4 KOTLIN: 3

Twitter: @allonsykraken Blog: krakendev.io
slide-37
SLIDE 37

TRUE TYPE ERASURE

Twitter: @allonsykraken Blog: krakendev.io
slide-38
SLIDE 38

SWIFT

protocol Pokemon { associatedType Type func attack() -> Type }

KOTLIN

interface Pokemon<out Type> { fun attack(): Type }

Twitter: @allonsykraken Blog: krakendev.io
slide-39
SLIDE 39

class PokemonTrainer { var favoritePokemon: Pokemon // Won't compile }

Twitter: @allonsykraken Blog: krakendev.io
slide-40
SLIDE 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
slide-41
SLIDE 41

GOOD OL' KOTLIN

data class Trainer(val favoritePokemon: Pokemon<Any>)

Twitter: @allonsykraken Blog: krakendev.io
slide-42
SLIDE 42

SWIFT: 4 KOTLIN: 4

Twitter: @allonsykraken Blog: krakendev.io
slide-43
SLIDE 43

INTEROPERABILITY

Twitter: @allonsykraken Blog: krakendev.io
slide-44
SLIDE 44

SWIFT: 4 KOTLIN: 5

Twitter: @allonsykraken Blog: krakendev.io
slide-45
SLIDE 45

VALUE/REFERENCE SEMANTICS

Twitter: @allonsykraken Blog: krakendev.io
slide-46
SLIDE 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
slide-47
SLIDE 47

// reference semantics class Person { let name: String } // value semantics struct Person { let name: String }

Twitter: @allonsykraken Blog: krakendev.io
slide-48
SLIDE 48

FLEXIBILITY/CONTROL

Twitter: @allonsykraken Blog: krakendev.io
slide-49
SLIDE 49

SWIFT: 5 KOTLIN: 5

Twitter: @allonsykraken Blog: krakendev.io
slide-50
SLIDE 50 Twitter: @allonsykraken Blog: krakendev.io
slide-51
SLIDE 51 Twitter: @allonsykraken Blog: krakendev.io
slide-52
SLIDE 52

SWIFT: 5 KOTLIN: 6

Twitter: @allonsykraken Blog: krakendev.io
slide-53
SLIDE 53

!!!

Twitter: @allonsykraken Blog: krakendev.io
slide-54
SLIDE 54

!

Twitter: @allonsykraken Blog: krakendev.io
slide-55
SLIDE 55

Twitter: @allonsykraken Blog: krakendev.io