Kotlin
pattern matching
when (var0) {
LITERAL -> // …
in 0..5 -> // …
is TYPE -> // …
else -> // …
}
nullability
declare nullable variable
var0: TYPE?
late-initialized variable
lateinit var var0: Type0
- no need for null check when accessing
- crash at runtime if no initialized
access field in nullable variable
return null
if var0
is null
using safe-call operator
var0?.field
assume not-null using non-null assertion operator
var0!!.field
return default_var0
if null
using Elvis operator
var0?.field ?: default_var0
anonymous function
refer to existing function
fun fn0(arg0: TYPE0): RETURN_TYPE0 {
// …
}
val lambda0: (TYPE0 -> RETURN_TYPE0) = ::fn0
create anonymous function directly
val lambda0: (TYPE0 -> RETURN_TYPE) = { arg0 ->
// …
}
or refer to the only argument as it
val lambda0 = {
doStuffWith(it) // …
}
call function using trailing lambda syntax
fnUsingLambda(arg0) {
// Body of anonymous function.
}
suspend function
suspend fun fn0(arg0: TYPE0): RETURN_TYPE0 {
// …
}
can only call in a
CoroutineScope
, execute seemingly sequentiallybrainless scope
GlobalScope
scope of current function
val scope = CoroutineScope(Job())
android UI component can use
val scope = MainScope()
can be started in background using
launch
orasync
of aCoroutineScope
use context to choose
Dispatcher
:withContext(Dispatchers.IO) {/*IO tasks*/} withContext(Dispatchers.Default) {/*CPU tasks*/}
Misc
prettry print Array
s: .contentToString()