The below example shows how we can use kotlin lazy in our application. The evaluation of the property of lazy is synchronized, and the value is computed only in one thread if the initialization is not required to allow multiple threads to execute simultaneously for the parameter of lazy. The call of subsequent will return the remembered result. The first call of getting is executing the lambda, which was passed through the lazy function, and we need to remember the result. Kotlin lazy is nothing but the function which was taking a lambda and returns an instance of lazy, which can serve as implementing the property of lazy. The concept of kotlin lazy initialization is designed to prevent the initialization of unnecessary objects as per the process. Sometimes we need to construct the object which contains the initialization of cumbersome. In kotlin, there is a number of classes whose initialization of the object is very heavy, and it will take time to result in a delay in the whole class process creation. The kotlin lazy is a very useful and important feature of property initialization we know when we are using which property. Kotlin lazy contains many features by using this feature, we can take advantage of writing better application.
0 Comments
Leave a Reply. |