kotlindelegatesparameter-passinglazy-evaluationdelegated-properties

How to pass Delegated property anonymously in Kotlin?


Let's assume a simple class:

class Foo(val p:String)

I would like to create instance of the class by passing lazy delegate:

val lazyP by lazy {"foo"}
println(Foo(lazyP).p)

but I don't need the boilerplate lazyP variable. How to write a one-liner? Something like:

println(Foo(by lazy {"foo"}).p)

Solution

  • I presume what you're trying to do is construct Foo and have p only calculated when it's first called? If so, I don't think that defining Foo as you've done will work, because when you construct Foo, the lazy delegate is called. That's because in order to construct Foo, a string is needed, so the lazy delegate is evaluated at that point. In other words, this:

    val lazyP by lazy { "foo" }
    val foo = Foo(lazyP)
    

    is more-or-less equivalent to:

    val foo = Foo("foo")
    

    I think that if you want Foo to have a lazy delegated property you should define it like this:

    class Foo(lazyP: Lazy<String>) {
        val p by lazyP
    }
    

    Then you can construct it in a one-liner without needing a separate variable:

    val foo = Foo(lazy { "foo" })