Taking Kotlin to production, Seriously

Preview:

Citation preview

Haim Yadid / Next Insurance

mapOf( taking?. kotlin to production )

Seriously!!

Disclaimer

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

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

❖ 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

Kotlin

Java

JavascriptNode.JS

Dropwizard

RDS (mysql)

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)

Kotlin Features

❖ Concise

❖ Safe

❖ Versatile

❖ Practical

❖ Interoperable

Why Kotlin

Java Scala Clojure

Groovy/JRuby

Java8

Strongly Typed

Loosely Typed

OO/verbose Functional/Rich

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

Why Kotlin

Java Scala Clojure

Groovy/JRuby

Java8

Strongly Typed

Loosely Typed

OO/verbose Functional/Rich

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

Scala can do it !! ?

❖ Yes Scala can do it

❖ Academic / Complicated

❖ Scala compile time is horrific

❖ Operator overloading -> OMG

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

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

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

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"

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") }

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"

❖ variables are declared using the keywords

❖ val (immutable)

❖ var (mutable)

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

val /var

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)

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

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}

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() } }

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

Safe operator ?.

❖ The safe accessor operator ?. will result null

❖ Elvis operator for default

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

Bang Bang !!

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

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

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}

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())}

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)

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)) }}

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.

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])

Kotlin 1.1

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

❖ Coroutines : async / await (stackless continuations)

❖ Generators: Yield

❖ inheritance of data classes

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 } }

Questions?