June 2018
Intermediate to advanced
316 pages
6h 34m
English
The concept of immutability was described in the Memory leaks section of Chapter 1, Identifying Performance Bottlenecks. In Kotlin, all classes and methods are final by default. If you want to inherit a class and override methods, you have to use the open keyword. For references, you can use the val keyword, but it doesn't guarantee immutability. The following example shows a case when the val keyword doesn't work:
interface ValueHolder<V> { val value: V}class IntHolder: ValueHolder<Int> { override val value: Int get() = Random().nextInt()}
In this example, the value property is read-only, but isn't immutable because the get() method returns different values all the time.
Since the popularity of Kotlin's multiplatform project ...
Read now
Unlock full access