Lazy

Let's consider the following example:

val lazyValue by lazy {    BigInteger.valueOf(2).modPow(BigInteger.valueOf(7), BigInteger.valueOf(20))}

The modPow method of the BigInteger class performs modular exponentiation. It's an extremely useful function in public-key cryptography. This calculation can take some time, which is why we wrapped it up in the Lazy<T> delegate.

The Lazy delegate supports lazy evaluation. It's a common strategy that delays invoking an expression until its value is needed and caches the value to avoid repeated evaluations. Using this strategy can increase performance and avoid error conditions during computation. 

The Lazy delegate supports three modes:

public enum class LazyThreadSafetyMode {    SYNCHRONIZED,    PUBLICATION, ...

Get Mastering High Performance with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.