49
Intro to Kotlin 15.10.2015@Infinum Dino Sulić & Tomislav Homan

Intro to kotlin

Embed Size (px)

Citation preview

Page 1: Intro to kotlin

Intro to Kotlin

15.10.2015@Infinum Dino Sulić & Tomislav Homan

Page 2: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 3: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 4: Intro to kotlin

Java on Android - the bad parts

● Java 7 was out 2011. - partial support 2013.

● Java 8 was out 2014. - no support announced in the near future

● Null references

● Covariant arrays

● Raw types

● SAM types

● Checked exceptions

Page 5: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 6: Intro to kotlin

Alternatives to Java● Xtend - Nice try, but very hard to work with in practice because

of IDE support

● Scala - We are engineers with deadlines, not scientists. Also 80k

methods

● Groovy - Very nice, but not type safe and adds over 30k methods.

IMHO best alternative - to our alternative :)

● Closure, etc, etc…

● Kotlin

Page 7: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 8: Intro to kotlin

Advantages of Kotlin (I)● Modern statically typed, object oriented language that borrows

many ideas from functional languages

● Fast learning curve - inspired by Scala and C#. Scaled down

Scala :)

● Meant to be used for building large software systems

● Engineered to be highly interoperable with Java - you can use

existing code base w/o (many) problems

Page 9: Intro to kotlin

Advantages of Kotlin (II)● The team behind it is JetBrains, creators of IntelliJ which is

Android Studio based on. Also has some contributions from

James Strachan - creator of Groovy

● Significantly more concise and readable than Java, which means

it’s easier to maintain

● Lightweight - Kotlin stdlib has 8k methods

● If it means something to someone - it also compiles to

Javascript

Page 10: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 11: Intro to kotlin

(Possible) disadvantages (I)● Code generation - After a long struggle works with, for now,

limited support - Dagger 2 works

● Mocking and testing - managed to run AndroidTestCase with

Mockito, but no serious testing done

● IDE was still bit unstable few months ago, but it seems fine now

and it can only get better. But still be patient for now

Page 12: Intro to kotlin

(Possible) disadvantages (II)● Java could break when using immutable collections returned

from Kotlin code

● New language that still evolves - teams need to establish code

conventions - still no "Effective Kotlin" :)

● Interoperability with new Jack and Jill compiler - should be all

right, but we have to monitor the situation

Page 13: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 14: Intro to kotlin

Features

Selection of features compared to Java

Page 15: Intro to kotlin

First thing that one will notice is function declaration includes keyword fun

Functions (I)

Page 16: Intro to kotlin

Parameter types are defined in Scala (Or Swift) way

Functions (II)

Page 17: Intro to kotlin

Default parameters come in handy

Functions (III)

Page 18: Intro to kotlin

Named parameters as a good substitution for builders

Functions (IV)

Page 19: Intro to kotlin

Functions are first-class Kotlin citizens

Functions (V)

Page 20: Intro to kotlin

Lambdas on Android? Not using Java

Functions (VI)

Page 21: Intro to kotlin

Lambdas on Android? Kotlin has them

Functions (VII)

Page 22: Intro to kotlin

Nice Retrofit example - cumbersome using Java

Functions (VIII)

Page 23: Intro to kotlin

Nice Retrofit example - pretty decent using Kotlin

Functions (IX)

Page 24: Intro to kotlin

Utility methods vs. extension functions

Functions (X)

Page 25: Intro to kotlin

Kotlin inferes types

Types (I)

Page 26: Intro to kotlin

It is difficult to write “final”. Kotlin makes it easier with val keyword

Types (II)

Page 27: Intro to kotlin

Java fields become Kotlin property. No need for getter / setters unless you want to override them

Properties (I)

Page 28: Intro to kotlin

If you have the need for some validation…

Properties (II)

Page 29: Intro to kotlin

...you can still override accessors and access backing field

Properties (III)

Page 30: Intro to kotlin

Nothing to say here

Null safety (I)

Page 31: Intro to kotlin

You have to explicitly use nullable type if you want to allow nulls

Null safety (II)

Page 32: Intro to kotlin

If you use nulls though, safe calls, Elvis and smart casts are here to help

Null safety (III)

Page 33: Intro to kotlin

Explicit vs. smart casts

Casting

Page 34: Intro to kotlin

In order to make this business logic right….

Data classes (I)

Page 35: Intro to kotlin

You have to do some work

Data classes (II)

Page 36: Intro to kotlin

In order to make this business logic right….

Data classes (III)

Page 37: Intro to kotlin

...you use data modifier

Data classes (IV)

Page 38: Intro to kotlin

Typical data repository

Collections (I)

Page 39: Intro to kotlin

Some business logic

Collections (II)

Page 40: Intro to kotlin

And lots of error prone work

Collections (III)

Page 41: Intro to kotlin

Same typical repository

Collections (IV)

Page 42: Intro to kotlin

Some business logic. Collections are really immutable

Collections (V)

Page 43: Intro to kotlin

But much less work

Collections (VI)

Page 44: Intro to kotlin

Just one more example...

Various (I)

Page 45: Intro to kotlin

...how Kotlin gets some things better

Various (II)

Page 46: Intro to kotlin

Out of scope● Generics - variance, covariance, contravariance

● Annotations

● Reflection

● Operator overloading

● Type-safe builders

● Dynamic types

● Java interop

● …and lot more

● http://kotlinlang.org/docs/reference/

Page 47: Intro to kotlin

Content

● Java on Android - the bad parts

● Alternatives to Java

● Advantages of Kotlin

● (Possible) disadvantages

● Features

● …

● Migration to Kotlin

Page 48: Intro to kotlin

Migration to Koltin● https://kotlinlang.org/docs/tutorials/kotlin-android.html

● Use convert code option - but check thoroughly afterwards

● Use clean code option after upgrading Kotlin version

● KotlinFrontEndException

● > sudo find . -name ".DS_Store" -depth -exec rm {} \;

● This list was much longer few months ago

Page 49: Intro to kotlin