Taking Kotlin to production, Seriously

33
Haim Yadid / Next Insurance mapOf( taking?. kotlin to production ) Seriously!!

Transcript of Taking Kotlin to production, Seriously

Page 1: Taking Kotlin to production, Seriously

Haim Yadid / Next Insurance

mapOf( taking?. kotlin to production )

Seriously!!

Page 2: Taking Kotlin to production, Seriously

Disclaimer

תוכנית זו הינה תוכנית סאטירה ואין לייחס לנאמר בהשום משמעות אחרת או ניסיון לפגוע בשפת תכנות כלשהיאם בכל זאת הנך חש נפגע מהתכנים המופיעים בה או אם אתה מתכנת בסקאלה אנו מתנצלים מראש

Page 3: Taking Kotlin to production, Seriously

About Me❖ Developing software since 1984 (Boy, am I getting old?)

❖ Basic -> Pascal -> C -> C++ -> Java -> Kotlin

❖ Architect in Mercury

❖ Independent performance expert for 7 years

❖ Backend developer in

Page 4: Taking Kotlin to production, Seriously

❖ Founded in the Beginning of 2016

❖ HQ@Palo Alto RnD@Kfar Saba

❖ Aims to disrupt the Small businesses insurance field

❖ Providing online experience which is simple fast and transparent

❖ We started to write real code on May 2016

Page 5: Taking Kotlin to production, Seriously

Kotlin

Java

JavascriptNode.JS

Dropwizard

RDS (mysql)

Page 6: Taking Kotlin to production, Seriously

What is Kotlin ?❖ Statically typed programming language

❖ for the JVM, Android and the browser (JS)

❖ 100% interoperable with Java™ (well almost)

❖ Developed by Jetbrains

❖ Revealed as an open source in July 2011

❖ v1.0 Release on Feb 2016

❖ 1.0.5 current stable version (28.11.2016)

Page 7: Taking Kotlin to production, Seriously

Kotlin Features

❖ Concise

❖ Safe

❖ Versatile

❖ Practical

❖ Interoperable

Page 8: Taking Kotlin to production, Seriously

Why Kotlin

Java Scala Clojure

Groovy/JRuby

Java8

Strongly Typed

Loosely Typed

OO/verbose Functional/Rich

)))))))))))))))))))))))))))))))

Page 9: Taking Kotlin to production, Seriously

Why Kotlin

Java Scala Clojure

Groovy/JRuby

Java8

Strongly Typed

Loosely Typed

OO/verbose Functional/Rich

Kotlin )))))))))))))))))))))))))))))))

Page 10: Taking Kotlin to production, Seriously

Scala can do it !! ?

❖ Yes Scala can do it

❖ Academic / Complicated

❖ Scala compile time is horrific

❖ Operator overloading -> OMG

Page 11: Taking Kotlin to production, Seriously

Getting Started

❖ Kotlin has very lightweight standard library

❖ Mostly rely on Java collections for good (interop) and for bad (all the rest)

❖ Kotlin compiles to Java6 byte code

❖ Has no special build tool just use Maven / Gradle

Page 12: Taking Kotlin to production, Seriously

Kotlin & Android

❖ Kotlin got widely accepted on the android community

❖ Runtime has low footprint

❖ Compiles to …. Java 6

❖ Brings lambdas to Android dev and a lot of cool features

Page 13: Taking Kotlin to production, Seriously

Trivial Stuff❖ No new keyword

❖ No checked exceptions

❖ == structural equality

❖ === referencial equality

❖ No primitives (only under the hood)

❖ No arrays they are classes

❖ Any ~ java.lang.Object

Page 14: Taking Kotlin to production, Seriously

Fun

❖ Functions are denoted with the fun keyword

❖ They can be member of a class

❖ Or top level functions inside a kotlin file

❖ can support tail recursion

❖ can be inlined

❖ fun funny(funnier: String) = "Hello world"

Page 15: Taking Kotlin to production, Seriously

Fun Parameters

❖ Pascal notation

❖ Function parameters may have default values

❖ Call to function can be done using parameter names

fun mergeUser(first: String = "a", last: String) = first + lastfun foo() { mergeUser(first="Albert",last= "Einstein") mergeUser(last= "Einstein") }

Page 16: Taking Kotlin to production, Seriously

Extension functions

❖ Not like ruby monkey patching

❖ Only extend functionality cannot override

❖ Not part of the class

❖ Static methods with the receiver as first a parameter

fun String.double(sep: String) = "$this$sep$this"

Page 17: Taking Kotlin to production, Seriously

❖ variables are declared using the keywords

❖ val (immutable)

❖ var (mutable)

fun funny(funnier: String): String { val x = "Hello world" return x }

val /var

Page 18: Taking Kotlin to production, Seriously

Classes

❖ less verbose than Java

❖ no static members (use companion object)

❖ public is default

❖ Must be declared open to be overridden

❖ override keyword is mandatory

class Producer(val name:String, val value: Int)

Page 19: Taking Kotlin to production, Seriously

Properties

❖ All members of classes are access via proper accessors (getter and setters)

❖ The usage of getters and setters is implicit

❖ one can override implementation of setters and getters

Page 20: Taking Kotlin to production, Seriously

Delegate Class

interface Foo { fun funfun()} class FooImpl(val x: Int) : Foo { override fun funfun() = print(x) } class Wrapper(f: Foo) : Foo by f fun main(args: Array<String>) { val f = FooImpl(10) val wf = Wrapper(f) wf.funfun() // prints 10}

Page 21: Taking Kotlin to production, Seriously

Smart Casts❖ If you check that a class is of a certain type no need to

cast it

open class A { fun smile() { println(":)")}} class B : A() { fun laugh() = println("ha ha ha") } fun funnier(a: A) { if (a is B ) { a.laugh() } }

Page 22: Taking Kotlin to production, Seriously

Null Safety❖ Nullability part of the type of an object

❖ Option[T] Optional<T>

❖ Swift anyone ?

var msg : String = "Welcome"msg = null

val nullableMsg : String? = null

Page 23: Taking Kotlin to production, Seriously

Safe operator ?.

❖ The safe accessor operator ?. will result null

❖ Elvis operator for default

fun funny(funnier: String?): Int? { println(x?.length ?: "") return x?.length}

Page 24: Taking Kotlin to production, Seriously

Bang Bang !!

❖ The bang bang !! throws an NPE if object is null

fun funny(funnier: String?): String { return funnier!! }

Page 25: Taking Kotlin to production, Seriously

Data classes❖ Less powerful version of to case classes in Scala

❖ Implementation of

❖ hashcode() equals()

❖ copy()

❖ de structuring (component1 component2 …)data class User(val firstName: String, val lastName: String)fun smile(user: User) { val (first,last) = user}

Page 26: Taking Kotlin to production, Seriously

When

❖ Reminds Scala’s pattern matching but not as strong

❖ More capable than select in java

when (x) { is Int -> print(x + 1) is String -> print(x.length + 1) is IntArray -> print(x.sum())}

Page 27: Taking Kotlin to production, Seriously

Sane Operator Overloading

❖ override only a set of the predefined operators

❖ By implementing the method with the correct name

Expression Translated to a + b a.plus(b) a - b a.minus(b) a * b a.times(b) a / b a.div(b) a % b a.mod(b) a..b a.rangeTo(b)

Page 28: Taking Kotlin to production, Seriously

Sane Operator Overloading

data class Complex(val x: Double, val y: Double) { operator fun inc(): Complex { return Complex(x + 1, y + 1) } infix operator fun plus(c: Complex): Complex { return (Complex(c.x + x, c.y + y)) }}

Page 29: Taking Kotlin to production, Seriously

Collections

❖ no language operators for construction of lists and maps

❖ using underlying java collections

❖ Have two versions mutable and immutable

❖ Immutability is a lie at least ATM it is only an immutable view of a mutable collection.

Page 30: Taking Kotlin to production, Seriously

Collections

val chars = mapOf("Terion" to "Lannister", "John" to "Snow") chars["Terion"] chars["Aria"] = “Stark"

val chars2 = mutableMapOf<String,String>()chars2["A girl"] = "has no name"

val sigils = listOf("Direwolf", "Lion", "Dragon", "Flower") println(sigils[0])

Page 31: Taking Kotlin to production, Seriously

Kotlin 1.1

❖ Java 7/8 support -> generate J8 class files

❖ Coroutines : async / await (stackless continuations)

❖ Generators: Yield

❖ inheritance of data classes

Page 32: Taking Kotlin to production, Seriously

Generators (yield)

val fibonacci: Sequence<Int> = generate { yield(1) // first Fibonacci number

var cur = 1 var next = 1

while (true) { yield(next) // next Fibonacci number

val tmp = cur + next cur = next next = tmp } }

Page 33: Taking Kotlin to production, Seriously

Questions?