typing and null-safety make a big difference. "Some people say Kotlin has 80% the power of Scala, with 20% of the features" * "Kotlin is a software engineering language in contrast to Scala which is a computing science language." * Swift and Kotlin are VERY similar. Swift is LLVM based and has C interop while Kotlin is JVM based and has Java interop. * Quotes from Kotlin: The Ying and Yang of Programming Languages @ sdeleuze #androiddev #kotlin #AndroidMakersFr @ekito
= 1 // `Int` type is inferred var x = 5 // `Int` type is inferred x += 1 Inferred values Mutable values val : constant value - IMMUTABLE var : variable value - MUTABLE USE VAL AS MUCH AS POSSIBLE ! #androiddev #kotlin #AndroidMakersFr @ekito
(stringB != null) stringB.length else -1 //or with the Elvis operator val length = stringB?.length ?: -1 Default Value & Elvis Operator val length = stringB.length // Compilation error - stringB can be null ! // use ? the safe call operator val length = stringB?.length //Value or null - length is type Int? val length = stringB!!.length // Value or explicit throw NPE - length is type Int Safe call with ?. or Explicit call with !!. #androiddev #kotlin #AndroidMakersFr @ekito
== 0 Properties can be declared in constructor or in class You can also handle getter & setter // customer getter & setter var stringRepresentation: String set(value) { … } class ApiKey(var weatherKey: String, var geocodeKey: String){ var debug : Boolean = false } * Late initialization, Lazy, Delegates … #androiddev #kotlin #AndroidMakersFr @ekito
Int = 0) val jack = User(name = "Jack", age = 1) //no new keyword val anotherJack = jack.copy(age = 2) Data Class usage A simple GSon Class #androiddev #kotlin #AndroidMakersFr @ekito
(s) { 1 -> print("x == 1") 2 -> print("x == 2") else -> { // Note the block print("x is neither 1 nor 2") } } Flow Control (replace your old if blocks) when (x) { in 1..10 -> print("x is in the range") in validNumbers -> print("x is valid") !in 10..20 -> print("x is outside the range") else -> print("none of the above") } Pattern Matching * can match with operators : in, as, is, range … #androiddev #kotlin #AndroidMakersFr @ekito
"b" to 2, "c" to 3) for ((k, v) in map) { println("$k -> $v") } map2["a"] = "my value" // direct map access with array style // range for (i in 1..100) { //... } // immutable list val list = listOf("a", "b", "c","aa") list.filter { it.startsWith("a") } * collections operators : map, filter, take, flatMap, forEach, firstOrNull, last … #androiddev #kotlin #AndroidMakersFr @ekito
// … } } fun WhatIsyourAge(user : User){ println("Your age is ${user.age}") } data class User (val name: String, val age : Int){ companion object{ fun sayHello(user : User){ //… } } } User u = new User("toto",42); User.Companion.sayHello(u); UserUtilsKt.WhatIsyourAge(u); UserSingleton.INSTANCE.stringify(u) #androiddev #kotlin #AndroidMakersFr @ekito
-> popLocationDialog(view) } A lambda expression or an anonymous function is a “function literal”, i.e. a function that is not declared, but passed immediately as an expression - A lambda expression is always surrounded by curly braces - Its parameters (if any) are declared before -> (parameter types may be omitted), - The body goes after -> (when present). myNumber.split("\n").flatMap { it.split(separator) } .map(Integer::parseInt) .map(::checkPositiveNumber) .filter { it <= 1000 } .sum() * Method references are not surrounded by curly braces ! #androiddev #kotlin #AndroidMakersFr @ekito