🏃‍♂️ Hızlı Notlar

🕐 Zaman Hesaplama

1
val time = measureTimeMillis {
2
val one = doSomethingUsefulOne()
3
val two = doSomethingUsefulTwo()
4
println("The answer is ${one + two}")
5
}
6
println("Completed in $time ms")
Copied!

📋 Hepsi

1
// TODO
2
fun calcTaxes(): BigDecimal = TODO("Waiting for feedback from accounting")
3
4
// Koşullu kullanım (Conditional expressions)
5
if (a > b) a else b
6
7
// Instance check
8
when (x) {
9
is Foo -> // ...
10
is Bar -> // ...
11
else -> // ...
12
}
13
(obj !is String)
14
15
// Range
16
for (x in 1..10 step 2) {}
17
18
// Collections
19
val fruits = listOf("banana", "avocado", "apple", "kiwifruit")
20
fruits
21
.filter { it.startsWith("a") }
22
.sortedBy { it }
23
.map { it.toUpperCase() }
24
.forEach { println(it) }
25
26
("[email protected]" !in emailsList)
27
28
// Filtreleme
29
list.filter { x -> x > 0 }
30
31
// Lazy
32
val p: String by lazy {
33
// compute the string
34
}
35
36
// Extension functions
37
fun String.spaceToCamelCase() { /* ... */ }
38
39
// Null
40
println(files?.size ?: "empty")
41
val mapped = value?.let { transformValue(it) } ?: defaultValue
42
val email = values["email"] ?: throw IllegalStateException("Email is missing!")
43
44
// Try-Catch
45
val email = values["email"] ?: throw IllegalStateException("Email is missing!")
46
47
fun transform(color: String): Int {
48
return when (color) {
49
"Red" -> 0
50
"Green" -> 1
51
"Blue" -> 2
52
else -> throw IllegalArgumentException("Invalid color param value")
53
}
54
}
55
56
fun test() {
57
val result = try {
58
count()
59
} catch (e: ArithmeticException) {
60
throw IllegalStateException(e)
61
}
62
63
// Working with result
64
}
65
66
fun transform(color: String): Int = when (color) {
67
"Red" -> 0
68
"Green" -> 1
69
"Blue" -> 2
70
else -> throw IllegalArgumentException("Invalid color param value")
71
}
72
73
// Java 7's try with resources
74
val stream = Files.newInputStream(Paths.get("/some/file.txt"))
75
stream.buffered().reader().use { reader ->
76
println(reader.readText())
77
}
78
79
// Convenient form for a generic function that requires the generic type information
80
81
// public final class Gson {
82
// ...
83
// public <T> T fromJson(JsonElement json, Class<T> classOfT) throws JsonSyntaxException {
84
// ...
85
86
87
inline fun <reified T: Any> Gson.fromJson(json: JsonElement): T = this.fromJson(json, T::class.java)
88
89
// Swap var
90
var a = 1
91
var b = 2
92
a = b.also { b = a }
93
94
Copied!

Functions vs Properties

In some cases functions with no arguments might be interchangeable with read-only properties. Although the semantics are similar, there are some stylistic conventions on when to prefer one to another.
Prefer a property over a function when the underlying algorithm:
  • does not throw
  • is cheap to calculate (or caсhed on the first run)
  • returns the same result over invocations if the object state hasn't changed

Platform types

A public function/method returning an expression of a platform type must declare its Kotlin type explicitly:
1
fun apiCall(): String = MyJavaApi.getProperty("name")
Copied!
Any property (package-level or class-level) initialised with an expression of a platform type must declare its Kotlin type explicitly:
1
class Person {
2
val name: String = MyJavaApi.getProperty("name")
3
}
Copied!
A local value initialized with an expression of a platform type may or may not have a type declaration:
1
fun main() {
2
val name = MyJavaApi.getProperty("name")
3
println(name)
4
}
Copied!

📝 Dokümantasyon Oluşturucu'

Auto generate Kotlin Android documentation with Dokka
Medium