40
Why Kotlin is your next language? theory and practice Aliaksei Zhynhiarouski 1 Dev Day MiniQ, 2016

Why Kotlin is your next language?

Embed Size (px)

Citation preview

Page 1: Why Kotlin is your next language?

Why Kotlin is your next language? theory and practice

Aliaksei Zhynhiarouski

1

Dev Day MiniQ, 2016

Page 2: Why Kotlin is your next language?

Kotlin Island2

Page 3: Why Kotlin is your next language?

Why is Kotlin• Static Typing bit.ly/st-vs-dyn

• Java + Kotlin = ❤. Effortless mixing both in one project

• Java Interoperability. 100% interoperable with Java.

3

Page 4: Why Kotlin is your next language?

Why is Kotlin• Null Safety

• Type Inference

• Immutability in the mind

• fun

4

Page 5: Why Kotlin is your next language?

fun main(args : Array<String>) {

println("Hello, MiniQ!")

}

5

Page 6: Why Kotlin is your next language?

val from_value : Type var from_variable : Type

val i_am_string = “mobile” var i_am_long = 666L val i_am_double = 3.14e10

6

val & var

Page 7: Why Kotlin is your next language?

Null Safety

// can’t be nullval foo: String = “Dev Day”

// need always perform the check val bar: String? = null

7

Type? = Type or null

Page 8: Why Kotlin is your next language?

Null Safetyval bar: String? = “Dev Day”

var a = bar?.length // 6var b = bar?.length?.inc() // 7var c = bar!!.length // can be NPE

8

Page 9: Why Kotlin is your next language?

Null Safetyval bar: String? = null

var a = if (bar != null) bar.length else 0 // 0var b = bar?.length ?: 0 // 0

9

Page 10: Why Kotlin is your next language?

Type Definition

class Phone(val brand: String)

10

• concise syntax for constructor • properties – not fields • final by default. Use open to open • simple old java class under the hood

Page 11: Why Kotlin is your next language?

Type Definition

class Phone(val brand: String){

var version: Int = 0

}

11

get() {…}private set

val phone = Phone(“Apple”) phone.brand == “Apple” // true

Page 12: Why Kotlin is your next language?

Value Object

data class Phone(val brand: String)

12

toString() hashCode() equals()

copy()

Page 13: Why Kotlin is your next language?

Get ready to be inspired13

Page 14: Why Kotlin is your next language?

Extensions

14

// Usage Example“MiniQ”.lastChar() // “Q”

// Definitionfun String.lastChar():Char = this.get(length - 1)

Page 15: Why Kotlin is your next language?

Extensions

15

// Java under the hood@file:JvmName(“StringUtils") StringUtils.lastChar(“Mobile”);

Page 16: Why Kotlin is your next language?

Extensions

16

Collections.max(list)↓

list.max()

Page 17: Why Kotlin is your next language?

Extensions

17

operator fun BigDecimal.inc() = this + BigDecimal.ONE

// Examplevar counter = BigDecimal.ZEROprint(++counter) // 1

Page 18: Why Kotlin is your next language?

Extensions

18

“25”.toInt() // Int

“8.$minor.5”.toOsVersion() // to object

Page 19: Why Kotlin is your next language?

Android Extensions

19

val text = find<TextView>(R.id.text)

Just extend Activity and have fun

inline fun <reified T : View> Activity.find(id: Int): T = this.findViewById(id) as T

final TextView text = (TextView) v.findViewById(R.id.text);

Page 20: Why Kotlin is your next language?

Lambdas

20

val boys = listOf( Boy(“Alex”), Boy(“Petr”), Boy(“Oleg”))

// 1 boys.filter({ b: Boy -> b.age > 18 })// 2boys.filter({ it.age > 18 })// 3boys.filter { it.age > 18 }

Page 21: Why Kotlin is your next language?

λ

21

boys.filter { it.age > 18 } .map { Pair(it, Boy("Eugene")) }.forEach { dance(it) }

// Definitioninline fun <T> Iterable<T>.filter (predicate: (T) -> Boolean)

Page 22: Why Kotlin is your next language?

λ

22

db.inTransaction { delete(“users”, “name = ?”, arrayOf(“Alex”)) }

fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

func()

setTransactionSuccessful() } finally { endTransaction() }}

Page 23: Why Kotlin is your next language?

Delegates – right way

23

class View {

}

val lazyProp by lazy { “Dev Day” }

val ops by observable(“”) { prop, old, new ->

print("$old to $new")

}

Page 24: Why Kotlin is your next language?

Delegates – wow way

24

class Activity {

}

private val btn: Button by lazy { findViewById(R.id.btn) as Button} override fun onCreate(savedBundle: Bundle?) { btn.setText(“MiniQ!”) }

Page 25: Why Kotlin is your next language?

Delegates – wow way

25

class Query(val props: Map<String, String>) {

}

val category by propsval model by propsval info: String get() { return “$category - $model” }

Page 26: Why Kotlin is your next language?

Delegates – wow way

26

class Query(val props: Map<String, String>) {

}

val category by propsval model by props…

val props = mapOf(“category” to “XXX”, “model” to “YYY”) val query = Query(props)

print(query.info) // “XXX - YYY”

Page 27: Why Kotlin is your next language?

DSL

27

• Type-safe

• Express your code by flexible syntax

• Ideal to build own query engine

• if/for/when just inside DSL

Page 28: Why Kotlin is your next language?

DSL

28

"(#C1 = :active) AND (#C2 = :type) AND " + "(attribute_not_exists(#C1) OR #C1 IN (:model, :allModels)) AND " + "...";if (smth)append("(#C2 = :active) AND NOT contains(#C3, :brandAll)");

// wait, oh shi~

Page 29: Why Kotlin is your next language?

DSL

29

val expr = filterExpr { group { eq("#1", ":2") and eq("#1", ":2") or group { eq("#2", ":2") if (smth) { and eq("#2", ":2") } } and …… }

Page 30: Why Kotlin is your next language?

One more DSL thing

30

Gradle 3 meets Kotlin

Page 31: Why Kotlin is your next language?

Kotlin 1.1

typealias Callback<T> = (T) -> T

// And nowfun alias(cb: Callback<String>) {}

31

Type aliases

Page 32: Why Kotlin is your next language?

Kotlin 1.1

typealias Table<K> = MutableMap<K, MutableList<String>>

32

Type aliases

Page 33: Why Kotlin is your next language?

Kotlin 1.1

val future = async<String> {

(1..5).map {

await(loooongAsyncOperation(it)) }.joinToString("\n")

}

33

Coroutines with async/await

Page 34: Why Kotlin is your next language?

Why is Kotlin• Use all existing Java frameworks and

libraries

• Code reviews are not a problem

• Suitable for enterprise Java

• Adopting is low risk

34

Page 35: Why Kotlin is your next language?

Why is Kotlin• Android

• Gradle

• Can be learned in a few hours

• Kotlin also compiles to JavaScript ;)

35

Page 36: Why Kotlin is your next language?

http://try.kotl.in

36

Page 37: Why Kotlin is your next language?

LinksAwesome Kotlin kotlin.link

Slack kotlinlang.slack.com

Local’s Chat gitter.im/JavaBy/Kotlin

37

Page 38: Why Kotlin is your next language?

Links just for youKotlin compilation speed

Performance: Kotlin Bytecode https://www.youtube.com/watch?v=eQ4YHpAXdp4

Experience of Square with Kotlin. Good analysis

Android Development advancedhttps://vimeo.com/144877458

Kotlin in real project with a lot of nuanceshttps://www.youtube.com/watch?v=CABN2r4GPpQ

38

Page 39: Why Kotlin is your next language?

Kotlin friends

39

jprof.by

bkug.by

Page 40: Why Kotlin is your next language?

Go v Minsk, ja sozdal