Lazy initialization in Kotlin with nullable types

Report a typo

In Kotlin, let's consider the following class with nullable types and member functions:


class ComplexCalculation {
     var value: Int? = null
     var valueSqr: Int? = null

     fun calculate(): Int { 
          // some complex calculations
          return result
     }

     fun calculateSqr(): Int { 
          // calculate square
          return square
     }
}
What are the correct ways to initialize both 'value' and 'valueSqr' lazily using respective functions inside 'ComplexCalculation' class instance 'compCalc'?
Select one or more options from the list

Create a free account to access the full topic