Getting Kotlin in Your Codebase
Eric Cochran
Oredev
November 10, 2016
Slide 2
Slide 2 text
Kotlin Value Add
Reduce Verbosity
Nullability
Lateinit
Data Classes
Mutability
Extension Functions
Slide 3
Slide 3 text
3
Reduce Verbosity
Lambdas
val listener: (View) -> Unit = { logClick() }
view.setOnClickListener (listener)
Slide 4
Slide 4 text
4
Reduce Verbosity
Default Parameters
fun print(list: List, count: Int = list.size())
Slide 5
Slide 5 text
5
Nullability
Nullability in the type system
var name: String? = null
Safe calls with ?.
val name: String? = user.name?.trim()
Assert not null with !!
val name: String = user.name!!
Compiler Tracking
if (user.name != null) return user.name.trim()
Slide 6
Slide 6 text
6
Some explanatory text to
introduce the theme of
the deck and such.
Lateinit
Available for non-null, var, non-primitive properties
Slide 7
Slide 7 text
7
Some explanatory text to
introduce the theme of
the deck and such.
Lateinit
Easy Dependency Injection
class MyView {
private lateinit var dependency: Dependency
fun setup(dependency: Dependency) {
this.dependency = dependency
}
}
Slide 8
Slide 8 text
8
Some explanatory text to
introduce the theme of
the deck and such.
Lateinit
Tests
public class MyViewTest {
private lateinit var view: MyView
@Setup fun setup() {
view = MyView(…)
view.setup(…)
}
}
Slide 9
Slide 9 text
9
data class Train(val number: Int, val name: String = “”,
val stations: List)
Generated equals, hashCode, copy
Allow implementing interfaces
Data Classes
Slide 10
Slide 10 text
10
Real immutable collections by default
List vs MutableList
Set vs MutableSet
Map vs MutableMap
Covariant mutable collections:
val textViews: List = listOf(name, badge)
val views: List = textViews
Mutability and Immutability