![]() Now we must have understood the lateinit vs lazy properties in Kotlin. It turns out that lazy is just a function It is a function that takes a single argument, a lambda, and returns an object. This was about the lazy property in Kotlin. Variable number of arguments (varargs) You can mark a parameter of a function (usually the last one) with the vararg. Must understand that it only creates the object when we access it for the very first time and then in the subsequent access, it returns the same object. Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).We want the variable to be initialized only if we need it for the first time. ![]() For JVM, Native Creates a new instance of the Lazy that uses the specified initialization function initializer and the default thread-safety mode LazyThreadSafetyMode.SYNCHRONIZED. Can be only used with the val keyword, hence read-only property. Creates a new instance of the Lazy that uses the specified initialization function initializer.Things to consider when we use the lazy property: It will get initialized when it is accessed for the first time. It will lead to the fast creation of the Session object in the above example because the Mentor object will not get initialized unnecessarily during the object creation of Session. So, we need to understand that the object mentor will get initialized only when it is accessed for the first time, else it will not get initialized.
0 Comments
Leave a Reply. |