https://kotlinlang.org logo
Title
f

forcelain

10/04/2018, 8:03 AM
private val items : MutableList<String> by lazy {
        // a lot of init code
    }
how do I exclude my initialisation code into a separate function? I can think of creating something like
private val items : MutableList<String> by lazy {
        init()
}
fun init() : MutableList<String> {...}
but maybe there is more elegant way
d

diesieben07

10/04/2018, 8:05 AM
You can use a function reference:
by lazy(::myFunction)
Or in this case,
by lazy(::init)
.
f

forcelain

10/04/2018, 8:07 AM
nice, thanks
but what if I need some params passed to the init func?
d

diesieben07

10/04/2018, 8:08 AM
Then you need to actually call it
f

forcelain

10/04/2018, 8:08 AM
ah, ok
thanks anyway 🙂
d

diesieben07

10/04/2018, 8:08 AM
You could use one of the existing FP libraries and then use
curry
. But I think that is a bit overkill here.