3. 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
4. Why is Kotlin
• Null Safety
• Type Inference
• Immutability in the mind
• fun
4
5. fun main(args : Array<String>) {
println("Hello, MiniQ!")
}
5
6. 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
7. Null Safety
// can’t be null
val foo: String = “Dev Day”
// need always perform the check
val bar: String? = null
7
Type? = Type or null
8. Null Safety
val bar: String? = “Dev Day”
var a = bar?.length // 6
var b = bar?.length?.inc() // 7
var c = bar!!.length // can be NPE
8
9. Null Safety
val bar: String? = null
var a = if (bar != null) bar.length
else 0 // 0
var b = bar?.length ?: 0 // 0
9
10. 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
11. Type Definition
class Phone(val brand: String){
var version: Int = 0
}
11
get() {…}
private set
val phone = Phone(“Apple”)
phone.brand == “Apple” // true
19. 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);
23. Delegates – right way
23
class View {
}
val lazyProp by lazy { “Dev Day” }
val ops by observable(“”) {
prop, old, new ->
print("$old to $new")
}
24. 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!”)
}
25. Delegates – wow way
25
class Query(val props: Map<String, String>) {
}
val category by props
val model by props
val info: String
get() {
return “$category - $model”
}
26. Delegates – wow way
26
class Query(val props: Map<String, String>) {
}
val category by props
val model by props
…
val props = mapOf(“category” to “XXX”,
“model” to “YYY”)
val query = Query(props)
print(query.info) // “XXX - YYY”
27. DSL
27
• Type-safe
• Express your code by flexible syntax
• Ideal to build own query engine
• if/for/when just inside DSL
28. 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~
29. 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 ……
}
33. Kotlin 1.1
val future = async<String> {
(1..5).map {
await(loooongAsyncOperation(it))
}.joinToString("n")
}
33
Coroutines with async/await
34. 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
35. Why is Kotlin
• Android
• Gradle
• Can be learned in a few hours
• Kotlin also compiles to JavaScript ;)
35
38. Links just for you
Kotlin compilation speed
Performance: Kotlin Bytecode
https://www.youtube.com/watch?v=eQ4YHpAXdp4
Experience of Square with Kotlin. Good analysis
Android Development advanced
https://vimeo.com/144877458
Kotlin in real project with a lot of nuances
https://www.youtube.com/watch?v=CABN2r4GPpQ
38