elegance of kotlin anton bannykh jetbrains · kotlin statically typed pragmatic concise safe...
TRANSCRIPT
![Page 2: Elegance of Kotlin Anton Bannykh JetBrains · Kotlin Statically typed Pragmatic Concise Safe Interoperable Tool-friendly 2](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/2.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/3.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/4.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/5.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/6.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/7.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/8.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/9.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/10.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/11.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/12.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/13.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/14.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/15.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/16.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/17.jpg)
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](https://reader030.vdocuments.net/reader030/viewer/2022041101/5edae3c509ac2c67fa687987/html5/thumbnails/18.jpg)
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