02 - Kotlin
Variables
Variable (mutable) - var xxx
Constant (immutable) – val yyy
Primary Constructor
The primary constructor is part of the class header
1 2 3 4 5 |
|
Initializer block(s)
The primary constructor has a constrained syntax, and cannot contain any code.
Initializer blocks are used for class setup
1 2 3 4 5 6 7 8 9 |
|
Secondary Constructor
Must not overload primary (different signature)
Must call primary constructor (if primary is defined)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Companion Object
Companion objects are singleton objects whose properties and functions are tied to a class but not to the instance of that class
Use @JvmStatic annotation to get true static bytecode in jvm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Nullable ref
- var b: String? Vs var a: String
- val l = if (b != null) b.length else -1
- Safe calls with ?.
- print(b?.length)
- b?.let { print(it) }
- Elvis operator - val l = b?.length ?: -1
- val l = b!!.length - not-null assertion
- Safe casts
- val aInt: Int? = a as? Int
Scoping functions
Executing block of code within the context of object. Call scoping function on object providing lambda expression - temporary scope is formed. In this scope you can access the object without its name.
Scope functions are
- let
let
takes the object it is invoked upon as the parameter (it
) and returns the result of the lambda expression.
1 2 3 4 5 6 7 8 9 10 |
|
- run
Similar to the let function, the run function also returns the last statement.
Unlike let, the run function doesn’t support the it
keyword.
The context object is available as a receiver (this
).
1 2 3 4 5 6 7 |
|
- with
A non-extension function: the context object is passed as an argument, but inside the lambda, it's available as a receiver (this
). The return value is the lambda result.
with is used to change instance properties without the need to call dot operator over the reference every time.
The last expression of with function returns a result.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
- apply
Unlike let
, it returns the original object instead of any new return data. Hence the return data has always the same type.
Runs on the object reference (receiver) in the expression and returns the object reference on completion.
1 2 3 4 5 6 7 8 9 |
|
- also
also
expressions does some additional processing on the object it was invoked.
Unlike let
, it returns the original object instead of any new return data. Hence the return data has always the same type.
Like let
, also uses it
too.
1 2 3 4 5 |
|
Scope functions, overview
- Executing a lambda on non-null objects: let
- Introducing an expression as a variable in local scope: let
- Object configuration: apply
- Object configuration and computing the result: run
- Running statements where an expression is required: non-extension run
- Additional effects: also
- Grouping function calls on an object: with
takeIf, takeUnless
In addition to scope functions, the standard library contains the functions takeIf and takeUnless. These functions let you embed checks of the object state in call chains.
1 2 3 4 |
|
Returns null on negative/other case