Hello guys. Does Kotlin support lazy init variable...
# android
s
Hello guys. Does Kotlin support lazy init variable with arguments ? In other words is it possible to simplify this construct ?
Copy code
object Utils {
        private var testDevices: Array<String>? = null

        private fun getTestDevices(context: Context): Array<String> {
            return testDevices ?: context.resources.getStringArray(R.array.testDevices)
        }
    }
r
private val testDevice by lazy { testDevices ?: context.resources.getStringArray(R.array.testDevices) }
This will be work. by lazy means you code will execute synchronize, second and later call only get from cache reference memory so this call will not execute again.