1 of 28

Kotlin 101

Dan Nizri

Android Developer @ Yodle

2 of 28

Structure of the talk

1 -> Intro to Kotlin

2 -> Language Features

4 -> Why Android?

3 -> Kotlin vs. Other Languages

5 -> Sample App

6 -> Summary & Sources

3 of 28

1 -> Intro to Kotlin

4 of 28

Why Kotlin?

According to JetBrains

5 of 28

2 -> Language Features

  • Lambda expressions + Inline functions = performant custom control structures
  • Extension functions
  • Null-safety
  • Smart casts
  • String templates
  • Properties
  • Primary constructors
  • First-class delegation
  • Type inference for variable and property types
  • Singletons
  • Declaration-site variance & Type projections
  • Range expressions
  • Operator overloading
  • Companion objects
  • Data classes
  • Separate interfaces for read-only and mutable collections

6 of 28

Model Objects

->

Data Classes

+ copy function

Limitations

  • Cannot be abstract, open, sealed or inner
  • Currently cannot extend other classes (but may implement interfaces) -> Kotlin 1.1

Java

Kotlin

7 of 28

Null Safety

  • The Billion Dollar Mistake
    • Tony Hoare introduced Null references in 1965 “simply because it was so easy to implement”
  • Kotlin eliminates NullPointerException
  • Null is a first class citizen
  • References must be declared as nullable or non-null (default)

8 of 28

Null Safety: Examples

9 of 28

Null Safety: Safe Calls + Chaining

Java

Kotlin

10 of 28

First Class Functions

Java

SAM Type Conversion (Single Abstract Method)

Kotlin

Function Type

11 of 28

Higher-Order Functions

  • Definition -> A function that does at least one of the following:
    • takes a function as an argument
    • returns a function as the result
  • map(), filter(), reduce(), etc.
  • Expressive -> what instead of how

12 of 28

Higher-Order Functions & Lambdas

Procedural

notBobNames = [Joe, Sally]

Functional

notBobNames = [Joe, Sally]

13 of 28

Extension Functions

  • Add functionality to existing class without:
    • Modifying the original type
    • Creating a new subclass
    • Using the Decorator design pattern
  • Any class (String, TextView, Context, etc.)
  • Resolved statically
  • No more Utils classes

14 of 28

Extension Functions: Example 1

15 of 28

Extension Functions: Example 2

Nullable Receiver

16 of 28

3 -> Kotlin vs. Other Languages

Java

    • More concise, more safe, first class function support

Scala

    • Less complex, better tooling, better interop, Gradle vs. sbt (Scala build tools), not a rewrite of Java

Clojure

    • Less functional, closer to Java

Swift

    • Similar syntax/features, Java interop, targets JVM

17 of 28

4 -> Why Android?

  • Fragmentation of Android OS
    • Java 8 release date -> March 2014
    • Kotlin can compile to Java 6, 7 or 8
  • Comparatively small standard library
    • ~10x smaller than Scala lib
    • Functions vs. SAM Types
    • Inline functions
  • Java Interop/Gradle Plugin
  • Kotlin Android Extensions

18 of 28

Kotlin Android Extensions: Synthetic Properties

19 of 28

Getting Started

20 of 28

5 -> Sample App

  • Dagger
  • Retrofit
  • RxKotlin
  • RxLifecycle
  • RxBinding
  • Picasso
  • Palette
  • RecyclerView
  • Custom View
  • Kotlin Android Extensions
  • Lambas & Higher-Order Functions
  • Data Classes
  • Extension Functions
  • Espresso & Unit Testing

21 of 28

6 -> Summary

Benefits

  • Concise
  • Safe
  • Productive
  • Expressive
  • Interoperable
  • IDE support

Risks

  • Less standard
  • Smaller community than Java
  • Not officially supported by Google
  • Build tools, Instant Run

22 of 28

Sources & Interesting Links

23 of 28

Yodle

Lighthouse Field Service

24 of 28

Questions?

Kotlin

Contact: daniel.nizri@yodle.com

25 of 28

Extra Slides

26 of 28

Basic Syntax

Function definition

Mutable local variable

Immutable local variable

Java

27 of 28

Extension Functions: Advanced Example

28 of 28

Anko Library