Access variable outside inline function in DSL design


#1

I’m using Anko for Android UI developlment, DSL design is awesome, but I can’t access inline block variable, I have to write code like this:

    var t: TextView? = null    // it's nullable
    var i: ImageView by Delegates.notNull()   // it's heavy and stupid.
    linearLayoutCompat {
        t = textView {
            TODO()
        }
        i = imageView {
            TODO()
        }
        val foobar = imageView {
            TODO()
        }
        //access inside variable foobar here is ok.

    }

    // using inside variable outside inline block.

    t!!.let {
            TODO()
    }

    //breaks non-null logical.
    t?.let {
            TODO()
    }

what if there is a feature that marks variable inside inline block can be accessed outside ?


#2

Starting with Kotlin 1.2 you can use lateinit local variables. This allows you to leave the initializer out and keep the type as non-null.


#3

Thanks, It’s nice feature.