Slide 1

Slide 1 text

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

Slide 11

Slide 11 text

11 Ending Util Hell @file:JvmName(“TextViewUtils”) fun TextView.isUserSet(user: User) = getText() == user.name Extension Functions

Slide 12

Slide 12 text

12 Measuring Performance var tock = measureTimeMillis { fetchAndSetNames() } log(tock)

Slide 13

Slide 13 text

13 Why Kotlin? Standard Library Scala: ~50K methods Groovy: ~30K methods Kotlin: ~6K methods

Slide 14

Slide 14 text

14 Convert to Kotlin Alt + Cmd + Shift + K

Slide 15

Slide 15 text

15 @Eric_Cochran github.com/NightlyNexus nightlynexus.com