No excuses, switch to kotlin

25
Kotlin No excuses, switch to Kotlin

Transcript of No excuses, switch to kotlin

Page 1: No excuses, switch to kotlin

KotlinNo excuses, switch to Kotlin

Page 2: No excuses, switch to kotlin

Thijs Suijten Mobile developer

Q42 @tsuijten

Page 3: No excuses, switch to kotlin
Page 4: No excuses, switch to kotlin
Page 5: No excuses, switch to kotlin
Page 6: No excuses, switch to kotlin
Page 7: No excuses, switch to kotlin

Getting everybody onboard

Page 8: No excuses, switch to kotlin

Getting everybody onboard

The team

Page 9: No excuses, switch to kotlin

Getting everybody onboard

Q42

Page 10: No excuses, switch to kotlin

Getting everybody onboard

PostNL (product owner)

Page 11: No excuses, switch to kotlin
Page 12: No excuses, switch to kotlin

fun reformat(str: String, normalize: Boolean = true, upperCase: Boolean = true): String { ...}

Functions

reformat("Hello world") reformat("Hello world", false, false) reformat("Hello world", upperCase = false)

// Single-Expression functions fun twiceTheFun(x: Int) = x * 2

Page 13: No excuses, switch to kotlin

class Person(val firstName: String, val age: Int, val locale: Locale = ENGLISH) { fun sayHello() = println("Hi $firstName!") }

Classes

val thijs = Person("Thijs", 34) thijs.sayHello() // Hi Thijs!

class Cat(name: String) { val name: String = name}

Page 14: No excuses, switch to kotlin

val x: String? = getSomeOptionalString()x.length // Does not compile.

Null safety / optionals

val y: String = null // Does not compile.

if (x != null) { x.length // Compiles!}

// Safe callval optionalLength = x?.length// Elvis operator.val length = x?.length ?: -1

Page 15: No excuses, switch to kotlin

Data classes

class User(val name: String, val age: Int)

val thijs = User("Thijs", 35) println(thijs) // User(name=Thijs, age=35)

val bday = thijs.copy(age = 36) // Named params!println(bday) // User(name=Thijs, age=36)

data

Page 16: No excuses, switch to kotlin

Data classespublic class User { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; if (age != user.age) return false; return name != null ? name.equals(user.name) : user.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

Page 17: No excuses, switch to kotlin

Functional / lambasval sum = listOf(1, 2, 3, 4, 5) .filter { number -> number < 5 } .map { it * 2 } .sum() println(sum) // 20

listOf("Zack", "Bob", "Thijs", "Julie", "Rob") .filter { it.length > 3 } .sorted().forEach { println(it) } // Julie Thijs Zack

Page 18: No excuses, switch to kotlin

When statement// Javaif (firstName.equals("Dan")) { person.setTeam(programmers); } else if (lastName.equals("Jamie")) { person.setTeam(designers); } else { person.setTeam(others); }

// Kotlinwhen { firstName == “Dan" -> person.team = programmers lastName == "Jamie" -> person.team = designers else -> person.team = others}

Page 19: No excuses, switch to kotlin

When statement// Javaswitch (firstName) { case "Dan": case "Jay": person.setTeam(programmers); break; case "Jamie": person.setTeam(designers); break; default: person.setTeam(others); }

// Kotlinwhen (firstName) { "Dan", "Jay" -> person.team = programmers "Jamie" -> person.team = designers else -> person.team = others}

Page 20: No excuses, switch to kotlin

When statementwhen (x) { 1 -> print("x = 1") is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum()) in 10..20 -> print("Between 10 and 20") }

val outcome = when(x) { 1 -> "x = 1" "Hello" -> "is it me you're looking for?" in 10..20 -> "Between 10 and 20"}

Page 21: No excuses, switch to kotlin

Extensions

// Extension function fun String.last(): Char { return this[lastIndex] }

println("Hello!".last())

// Extension property val String.first: Char get() = this[0] println("Hello! ".first) // H

Page 22: No excuses, switch to kotlin

Kotlin Android extensions

<TextView android:id="@+id/greeting" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me!"/>

override fun onCreate(savedInstanceState: Bundle?) { //… greeting.text = "Hello" button.onClick { toast("Button clicked!") } }

Page 23: No excuses, switch to kotlin

Kotlin

Page 24: No excuses, switch to kotlin
Page 25: No excuses, switch to kotlin

..

[email protected] / @tsuijten