Next Insurance was founded in the beginning of 2016 and first lines of our production code started accumulating in May 2016. In the beginning I have started writing in Java and experimenting with Kotlin, which saw its 1.0 release two months earlier. 6 months later, the development of our backend services has totally shifted to Kotlin. We still keep a few classes in Java just to make sure that the integration remains seamless but the vast majority of our codebase is written in Kotlin. In this talk I will cover the language features and why I think it is awesome, from null safety to smart casts and data classes. We will also look into the future with 1.1 async/await feature and more.
3. 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
4. ❖ 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
6. 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)
10. Scala can do it !! ?
❖ Yes Scala can do it
❖ Academic / Complicated
❖ Scala compile time is horrific
❖ Operator overloading -> OMG
11. 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
12. 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
13. 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
14. 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"
15. 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 + last
fun foo() {
mergeUser(first="Albert",last= "Einstein")
mergeUser(last= "Einstein")
}
16. 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"
17. ❖ variables are declared using the keywords
❖ val (immutable)
❖ var (mutable)
❖
fun funny(funnier: String): String {
val x = "Hello world"
return x
}
val /var
18. 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)
19. 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
20. 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
}
21. 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()
}
}
22. 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
23. Safe operator ?.
❖ The safe accessor operator ?. will result null
❖ Elvis operator for default
fun funny(funnier: String?): Int? {
println(x?.length ?: "")
return x?.length
}
24. Bang Bang !!
❖ The bang bang !! throws an NPE if object is null
fun funny(funnier: String?): String {
return funnier!!
}
25. 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
}
26. 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())
}
27. 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)
28. 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))
}
}
29. 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.
30. 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])
31. Kotlin 1.1
❖ Java 7/8 support -> generate J8 class files
❖ Coroutines : async / await (stackless continuations)
❖ Generators: Yield
❖ inheritance of data classes
32. 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
}
}