76
Svetlana Isakova You can do better with Kotlin

You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

  • Upload
    others

  • View
    33

  • Download
    0

Embed Size (px)

Citation preview

Page 1: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Svetlana Isakova

You can do better with Kotlin

Page 2: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

- modern - pragmatic - Android-friendly

Kotlin Programming Language

Page 3: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Official on Android

Page 4: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Not only Android

Page 5: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Pragmatic

- tooling - Java interop

Page 6: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

From

Page 7: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

has good tooling

Page 8: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

- completion - navigation - refactorings - inspections

Page 9: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

can be easily mixed with Java code

Page 10: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

*.java

*.class

*.dex

compiled to Java bytecode

*.kt

Page 11: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Kotlin code

Java code

You can have Java & Kotlin code in one project

Page 12: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

You can gradually add Kotlin to your existing app

Page 13: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Android-friendly

Page 14: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Android Studio is based on IntelliJ IDEA

Page 15: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

just another library for your app

rxjava-2.1.2

kotlin-stdlib-1.1.4 6315

10212

Page 16: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

No Kotlin SDK…just JDK + extensions

small runtime jar easy Java interop

Page 17: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Modern

- concise - safe - expressive

Page 18: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

concise

Page 19: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}

Page 20: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

- equals - hashCode - toString

data

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

Page 21: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}

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

person.nameperson.getName()

Page 22: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

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

person.getName()

Page 23: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; }}

person.name

Page 24: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

public void updateWeather(int degrees) { String description; Colour colour; if (degrees < 5) { description = "cold"; colour = BLUE; } else if (degrees < 23) { description = "mild"; colour = ORANGE; } else { description = "hot"; colour = RED; } // ...}

enum Colour { BLUE, ORANGE, RED, /*...*/; }

Page 25: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun updateWeather(degrees: Int) { val description: String val colour: Colour if (degrees < 5) { description = "cold" colour = BLUE } else if (degrees < 23) { description = "mild" colour = ORANGE } else { description = "hot" colour = RED } // ...}

Page 26: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun updateWeather(degrees: Int) { val (description: String, colour: Colour) = if (degrees < 5) { Pair("cold", BLUE) } else if (degrees < 23) { Pair("mild", ORANGE) } else { Pair("hot", RED) } // ...}

Page 27: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun updateWeather(degrees: Int) { val (description, colour) = if (degrees < 5) { Pair("cold", BLUE) } else if (degrees < 23) { Pair("mild", ORANGE) } else { Pair("hot", RED) } // ...}

Page 28: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun updateWeather(degrees: Int) { val (description, colour) = when { degrees < 5 -> Pair("cold", BLUE) degrees < 23 -> Pair("mild", ORANGE) else -> Pair("hot", RED) } // ...}

Page 29: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun updateWeather(degrees: Int) { val (description, colour) = when { degrees < 5 -> "cold" to BLUE degrees < 23 -> "mild" to ORANGE else -> "hot" to RED } }

Page 30: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val (description, colour) = when { degrees < 5 -> "cold" to BLUE degrees < 23 -> "mild" to ORANGE else -> "hot" to RED}

String description;Colour colour; if (degrees < 5) { description = "cold"; colour = BLUE; } else if (degrees < 23) { description = "mild"; colour = ORANGE; } else { description = "hot"; colour = RED; }

Page 31: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

safe

Page 32: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Billion Dollar Mistake

Page 33: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Modern approach: to make NPE

compile-time error, not run-time error

Page 34: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Nullable types in Kotlinval s1: String = "always not null" val s2: String? = null

s1.length ✓

✗s2.length

"can be null or non-null"

null ✗

Page 35: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val s: String?

if (s != null) { s.length}

Dealing with Nullable Types

Page 36: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

s?.length

val s: String?

Dealing with Nullable Types

Page 37: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val length = if (s != null) s.length else null

val s: String?

Nullability operators

val length = s?.length

Page 38: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val length = if (s != null) s.length else 0

val s: String?

Nullability operators

val length = s?.length ?: 0

Page 39: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val s: String?

if (s == null) fail() s.length

Control-flow analysis

Page 40: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val s: String?

Making NPE explicit

s!!throws NPE if s is null

s!!.length

Page 41: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Nullable Types Under the Hood

No performance overhead

@Nullable, @NotNull annotations

Page 42: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Annotate your Java types in KotlinType behaves like

regular Java type

@Nullable

@NotNull Type

Type?

Type

Type

@ParametersAreNonnullByDefault

@MyNonnullApiType/Type?

Page 43: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

class Optional<T>(val value: T) { fun isPresent() = value != null fun get() = value ?: throw NoSuchElementException("No value present") }

Nullable types ≠ Optional

Page 44: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

expressive

Page 45: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

you can avoid any repetition

you can make the code look nicer

you can create API looking like DSL

expressive

Page 46: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Extension Functions

Page 47: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun String.lastChar() = get(length - 1)

this can be omitted

Extension Functionsfun String.lastChar() = this.get(this.length - 1)

Page 48: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

import com.example.util.lastCharimport com.example.util.*

Extension Functions

val c: Char = "abc".lastChar()

fun String.lastChar() = get(length - 1)

Page 49: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

fun String.lastChar() = get(length - 1)

Calling Extension Functions from Java code

StringExtensions.kt

char c = StringExtensionsKt.lastChar("abc");JavaClass.java

import static StringExtensionsKt.lastChar; char c = lastChar("abc");

Page 50: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

No. Because it’s a regular static method under the hood.

fun String.lastChar() = get(length - 1)

Extension Functions

Is it possible to call a private member of String here?

Page 51: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Extensions for Android Toast.makeText(this, "Thank you!”, Toast.LENGTH_SHORT).show()

Activity

extension function on Activity

toast("Thank you!")

this.toast("Thank you!")

Page 52: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

this.startActivity<NewActivity>(“ANSWER" to 42)

val intent = Intent(this, NewActivity::class.java) intent.putExtra("ANSWER", 42) startActivity(intent)

Extensions for Android

Page 53: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

infix fun <A, B> A.to(that: B) = Pair(this, that)

"ANSWER".to(42)

"hot" to RED

mapOf(0 to "zero", 1 to "one")

The to extension function

Page 54: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Lambdas

Page 55: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Lambdas

button.addActionListener { println("Hi") }

Page 56: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

{ employee: Employee -> employee.city == City.PRAGUE }

What’s an average age of employees

working in Prague?

Working with collections with Lambdasval employees: List<Employee>

employees.filter { it.city == City.PRAGUE }.map { it.age }.average()

data class Employee( val city: City, val age: Int )

Page 57: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

What’s an average age of employees

working in Prague?

Working with collections with Lambdasval employees: List<Employee>

data class Employee( val city: City, val age: Int )

extension functions

employees.filter { it.city == City.PRAGUE }.map { it.age }.average()

Page 58: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Kotlin library: extensions on collections

• filter • map • reduce

• count • find • any

• flatMap • groupBy • …

Page 59: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Under the Hood

No performance overheadLambdas can be inlined

Page 60: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Extension Function & Lambda

Lambda with receiver

Page 61: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val sb = StringBuilder()with (sb) { appendln("Alphabet: ") for (c in 'a'..'z') { append(c) } toString()}

The with function

with is a function

val sb = StringBuilder()sb.appendln("Alphabet: ") for (c in 'a'..'z') { sb.append(c)} sb.toString()

Page 62: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

lambda is its

second argument

val sb = StringBuilder()with (sb) { this.appendln(“Alphabet: ") for (c in 'a'..'z') { this.append(c) } this.toString()}

val sb = StringBuilder()with (sb, { -> this.appendln(“Alphabet: ") for (c in 'a'..'z') { this.append(c) } this.toString()})

lambda is its

second argument

Lambda with receiverwith is a function

this is an implicit receiver

in the lambda

val sb = StringBuilder()with (sb) { appendln("Alphabet: ") for (c in 'a'..'z') { append(c) } toString()}

this can be omitted

Page 63: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

with (sb) { appendln("Alphabet: ") ...}

inline fun <T, R> with( receiver: T, block: T.() -> R ): R = receiver.block()

The with function declaration

Page 64: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Lambda with receiver

val sb = StringBuilder()with (sb) { appendln("Alphabet: ") for (c in 'a'..'z') { this.append(c) }}

lambda with implicit this

Page 65: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

html { table { for (product in products) { tr { td { text(product.description) } td { text(product.price) } td { text(product.popularity) } } } } }

HTML Builderslambdas with receiver

Page 66: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

val db: SQLiteDatabase = … db.beginTransaction()try { db.delete("users", "first_name = ?", arrayOf("Jake")) db.setTransactionSuccessful()} finally { db.endTransaction()}

db.inTransaction { delete("users", "first_name = ?", arrayOf("Jake"))}

Avoiding Duplication

Page 67: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

db.beginTransaction()try { db.delete("users", "first_name = ?", arrayOf("Jake")) db.setTransactionSuccessful()} finally { db.endTransaction()}

db.inTransaction { delete("users", "first_name = ?", arrayOf("Jake"))}

Inline functionsis declared as

inline function

generated bytecode is similar to

Page 68: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

ANKO-DSLDSL for dynamic layouts

Page 69: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Alertsfun Activity.showAreYouSureAlert(process: () -> Unit) { alert(title = "Are you sure?", message = "Are you really sure?") { positiveButton("Yes") { process() } negativeButton("No") { } }.show()}

Are you sure?

Are you really sure?

No Yes

Page 70: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

customView { verticalLayout { val email = editText { hint = "Email" } val password = editText { hint = "Password" transformationMethod = PasswordTransformationMethod.getInstance() } positiveButton("Log In") { logIn(email.text, password.text) } }}

Password

Log In

Email

Custom layouts

Page 71: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

kotlinlang.org

Page 72: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Gradle & Kotlin

Writing Gradle build scripts and plugins in Kotlin

Page 73: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

try.kotlinlang.org

Page 74: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Kotlin Koans

Page 75: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only
Page 76: You can do better with Kotlin - YOW! Conferences€¦ · You can do better with Kotlin-modern -pragmatic -Android-friendly Kotlin Programming Language. Official on Android. Not only

Have a nice Kotlin!