Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe...

18
Elegance of Kotlin Anton Bannykh JetBrains goo.gl/RcE9iT

Transcript of Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe...

Page 1: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Elegance of Kotlin

Anton BannykhJetBrains

goo.gl/RcE9iT

Page 2: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Kotlin➔ Statically typed➔ Pragmatic➔ Concise➔ Safe➔ Interoperable➔ Tool-friendly

2https://kotlinlang.org/

Page 3: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Example➔ Android➔ Text on the screen➔ Contains an integer➔ Task: increment it

public class TextView … {

public CharSequence getText() {...}

public final void setText(CharSequence text) {...}

}

3

Page 4: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

4

fun increment(view: View) { val incView = findViewById(R.id.textView) as TextView val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value)}

Kotlin

Page 5: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

5

import kotlinx.android.synthetic.main.activity_main.*...fun increment(view: View) { val incView = textView val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value)}

Kotlin

Page 6: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

6

fun increment(view: View) { val incView = textView val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value)}

Kotlin

Page 7: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

7

fun increment(view: View) { val value = Integer.parseInt(textView.text.toString()) + 1 textView.text = Integer.toString(value)}

Kotlin

Page 8: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

8

fun increment(view: View) { intView++}

var intView: Int get() = textView.text.toString().toInt() set(v) { textView.text = v.toString() }

Kotlin

Page 9: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

9

fun increment(view: View) { textView.intText++}

var TextView.intText: Int get() = text.toString().toInt() set(v) { text = v.toString() }

Kotlin

Page 10: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

10

fun increment(view: View) { textView.intText++}

Kotlin

Page 11: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

11

fun increment(view: View) = textView.intText++

Kotlin

Page 12: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

12

fun increment(view: View) { val incView = findViewById(R.id.textView) if (incView is TextView) { val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value) } else throw Error()}

Kotlin Extrafun increment(view: View) { val incView = findViewById(R.id.textView) if (incView is TextView) { val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value) } else throw Error()}

public void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

Page 13: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Javapublic void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

13

fun increment(view: View) { val incView = findViewById(R.id.textView) as? TextView ?: throw Error() val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value)}

Kotlin Extrafun increment(view: View) { val incView = findViewById(R.id.textView) as? TextView ?: throw Error() val value = Integer.parseInt(incView.text.toString()) + 1 incView.text = Integer.toString(value)}

public void increment(@NonNull View view) { TextView incView = (TextView) findViewById(R.id.textView); int value = Integer.parseInt(incView.getText().toString()) + 1; incView.setText(Integer.toString(value));}

Page 14: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

AdvancedDelegate var intView by intViewDelegate(textView)

Standard library

data class Person(val name: String, val age: Int) val people = listOf(Person("John", 30)) for ((name, age) in people) { println("$name is $age years old") }

val adults = people.filter { it.age > 21 }

14

Page 15: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

AdvancedDSL

verticalLayout {

val name = editText()

button("Say Hello") {

onClick { toast("Hello, ${name.text}!") }

}

}

15https://github.com/Kotlin/anko

Page 16: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Bleeding edgeCoroutines

async(UI) {

val data: Deferred<Data> = bg {

// Runs in background

getData()

}

// This code is executed on the UI thread

showData(data.await())

}

16https://kotlinlang.org/docs/reference/coroutines.html

Page 17: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Bleeding edgeMultiplatform

● JVM● JS

○ EcmaScript 5

● Native○ Tech preview○ Based on LLVM○ Cool demos!

17

Page 18: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly  2

Linkskotlinlang.org - documentation and marketing

try.kotl.in - try online, complete koans

github.com/Kotlin/anko - goodies for Android development

github.com/Kotlin/kotlinx.html - isomorphic HTML DSL

github.com/Kotlin/kotlinx.coroutines - official coroutine support library

18