https://kotlinlang.org/docs/reference/idioms.html#string-interpolation
println("Name $name") |
Prefer read-only `val` over mutable `var`
/* bad */ var counter = 0 for(i in 0..20){ counter += i } /* preferred */ val sum = (0..20).sum() |
Unit as a return type
fun doSomething(): Unit{ println("do something") } |
https://kotlinlang.org/docs/reference/idioms.html#single-expression-functions
fun theAnswer() = 42 |
https://kotlinlang.org/docs/reference/idioms.html#default-values-for-function-parameters
fun foo(a: Int = 0, b: String = "") { ... } |
https://kotlinlang.org/docs/reference/idioms.html#creating-a-singleton
object Resource { val name = "Name" } |
https://kotlinlang.org/docs/reference/idioms.html#instance-checks
when (x) { is Foo -> ... is Bar -> ... else -> ... } |
https://kotlinlang.org/docs/reference/idioms.html#creating-dtos-pojospocos
data class Customer(val name: String, val email: String) |
Use the fact that `return` and `throw` have type `Nothing`, https://kotlinlang.org/docs/reference/idioms.html#todo-marking-code-as-incomplete
val x = foo() ?: return notFound() val y = if(conditionSatisfied()){ 1.0 } else { error("Condition is not satisfied"}//throws inside, the type is Nothing } |
Nullable truth.
a?.b == true //instead of a?.b ?: false |
var x: String?; x = x ?: "Hello"; (analog for dart ?=)
var x: String? = null x = x ?: "Hello" |
Safe chain and elvis (https://kotlinlang.org/docs/reference/idioms.html#if-not-null-and-else-shorthand)
val files = File("Test").listFiles() println(files?.size ?: "empty") |
https://kotlinlang.org/docs/reference/idioms.html#execute-if-not-null
val value = ... value?.let { ... // execute this block if not null } |
https://kotlinlang.org/docs/reference/idioms.html#extension-functions
fun String.spaceToCamelCase() { ... } "Convert this to camelcase".spaceToCamelCase() |
Extension properties
import java.util.Calendar var Calendar.year get() = get(Calendar.YEAR) set(value) = set(Calendar.YEAR, value) val calendar = Calendar.getInstance() println(calendar.year) // 2020 calendar.year = 2021 println(calendar.year) // 2021 |
if ("john@example.com" in emailsList) { ... } if ("jane@example.com" !in emailsList) { ... } |
for ((k, v) in map) { println("$k -> $v") } val (a,b) = Pair(1, 2) |
for (i in 1..100) { ... } // closed range: includes 100 for (i in 1 until 100) { ... } // half-open range: does not include 100 for (x in 2..10 step 2) { ... } for (x in 10 downTo 1) { ... } if (x in 1..10) { ... } |
val list = listOf("a", "b", "c") |
println(map["key"]) map["key"] = value |
val p: String by lazy { // compute the string } |
fun transform(color: String): Int { return when (color) { "Red" -> 0 "Green" -> 1 "Blue" -> 2 else -> throw IllegalArgumentException("Invalid color param value") } } |
fun test() { val result = try { count() } catch (e: ArithmeticException) { throw IllegalStateException(e) } // Working with result } |
fun arrayOfMinusOnes(size: Int): IntArray { return IntArray(size).apply { fill(-1) } } |
val myRectangle = Rectangle().apply { length = 4 breadth = 5 color = 0xFAFAFA } |
val stream = Files.newInputStream(Paths.get("/some/file.txt")) stream.buffered().reader().use { reader -> println(reader.readText()) } |
// public final class Gson { // ... // public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException { // ... inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T::class.java) |
fun foo() { listOf(1, 2, 3, 4, 5).forEach { if (it == 3) return // non-local return directly to the caller of foo() print(it) } println("this point is unreachable") } |
val list: List<Int> = listOf(1,2,3,4,5,6) val result = list .filter{it%2 == 0} //select even numbers .map{it*it} // get square of each element .sumByDouble{it.toDouble()} //use one of reduce operations |
class Doubles(val list: DoubleArray) @Suppress("FunctionName") fun Doubles(vararg numbers: Number) = Doubles(numbers.map{it.toDouble()}.toDoubleArray()) Doubles(1,2,3) |
var a: Int = 2 protected set // accessible only in descendents |
ptivate val _list = ArrayList<Int>() val list: List<Int> get() = _list |
fun integrate(from: Double = 0.0, to: Double = 1.0, function: (Double)→Double): Double {...} integrate{x→x*x + 2*x + 1} integrate(0.0, Pi){ sin(it) } |
val a = if(b>0) 0 else 1 |
val ioResult: IOResult? = try{ readFromFile() //assign result if it read is successful } catch(t: FileNotFoundException){ null // return null if file not found } |
val map: MutableMap<String, Int> val (a, b) = readlLine()?.split(" ") ?: error("No line to read") // read line and split it by given delimeter and use destructuring declaration map[a] = b.toInt() // Convert the value on-flight and assign it |
fun doSomething(additionalArguments: List<String> = emptyList()) |