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
-Dfile.encoding=UTF-8 org.gradle.parallel=true org.gradle.deamon=true # Kotlin kotlin.incremental=true # Android Studio 2.2+ android.enableBuildCache=true In your gradle.properties https://medium.com/keepsafe-engineering/kotlin-vs-java-compilation-speed-e6c174b39b5d#.k44v7axsk
: variable value - MUTABLE val icon1 : String = "sunny" icon1 = "cloudy" // error - val cannot be reassigned var icon2 = "sunny" icon2 = "cloudy" USE VAL AS MUCH AS POSSIBLE ! TYPE INFERENCE
val temperatureLow: String, val temperatureHigh: String) Optional body Public/Closed by default val forecast = DailyForecastModel("description ...","sunny-icon","5°C","18°C") POJO + - Getter - Setter - Constructors
upperCaseFirstLetter: Boolean = true, wordSeparator: Char = ' ') : String { } - Parameters are IMMUTABLE (considered as VAL) - Parameter has a NAME & can have DEFAULT VALUE - Return type can be optional (single expression) reformat(str, true, true, '_') // old way to call reformat(str, wordSeparator = '_') // using default values & named params
Geocode): Location = ... fun getDailyForecasts(weather: Weather): List<DailyForecastModel> { ... } } Singleton Class data class DailyForecastModel(val forecastString: String, val icon: String, val temperatureLow: String, val temperatureHigh: String) { val temperatureString = "$temperatureLow°C - $temperatureHigh°C" companion object{ fun sunnyDay() = DailyForecastModel("It's a good day today !","sunny","16°C","25°C") } } Companion Object
forecast2 : DailyForecastModel? = null val forecast : DailyForecastModel = null // Error because type is a non- null type val forecast : DailyForecastModel? = … - Nullable type : accept null value - Type is nullable when followed by ? You must use proper operators with nullable types !
= mapOf("a" to 1, "b" to 2, "c" to 3) list.filter { it.startsWith("a") } Collections operators with lambdas : map, filter, take, flatMap, forEach, firstOrNull, last … - Based on Java Collections, but add some smart API - Mutable / Immutable Collections // range for (i in 1..100) { //... } Range expression for for-loop map["a"] = "my value" // direct map access with array style for ((k, v) in map) { println("$k -> $v") } Smart accessors
-> print("x == 2") else -> { // Note the block print("x is neither 1 nor 2") } } Flow Control (replace your switch and 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
-> 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 - parameters are declared before -> (parameter types may be omitted) - 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 parenthesis !
forecast?.simpleforecast?.forecastday.orEmpty() .map { f -> DailyForecastModel(f.conditions!!, getWeatherCode(f.icon!!), f.low!!.celsius!!, f.high!!.celsius!!) } .filter { f -> !f.icon.startsWith(PREFIX) } .take(4) Old style … val forecasts = WeatherSDKUtil.getDailyForecasts(weather) val forecasts = weather.getDailyForecasts() Using extension
value) -> "$value!" } Destructured variables declaration val (_, _, low, high) = DailyForecastModel("description ...","sunny- icon","5°C","18°C") println("Today we have $low < $high") Works with Map, Data Class, Pair, Triple …
Great learning curve - Good doc & tools support - Don’t use anymore Butterknife, Dagger … - ⚠ Tests Mocks, Realm DB … - Hard to come back to Java https://www.ekito.fr/people/kotlin-in-production-one-year-later/