Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin how to refer outer-scope this in multi-layer apply functions

for example:

 v1?.apply {
       v2?.apply {
           call(this, target, outerThis);
       }
    }

my question is how to refer to "outerThis"? thanks for any help.

like image 250
Jack Zhu Avatar asked Jun 27 '17 11:06

Jack Zhu


People also ask

Can you chain scope functions together in Kotlin?

Hence, they can be included into call chains as side steps: you can continue chaining function calls on the same object after them. They also can be used in return statements of functions returning the context object.

What does :: mean in Kotlin?

:: is just a way to write a lambda expression basically we can use this to refer to a method i.e a member function or property for example class Person (val name: String, val age: Int) Now we can write this to access the person which has the maximium age.

What is this () in Kotlin?

In Kotlin, the “this” keyword allows us to refer to the instance of a class whose function we happen to be running.

What does .apply do in Kotlin?

Kotlin apply is an extension function on a type. It runs on the object reference (also known as receiver) into the expression and returns the object reference on completion.


1 Answers

You can use a label and then a qualified this expression:

v1?.apply outer@ {
    v2?.apply {
        call(this, target, this@outer)
    }
}
like image 137
zsmb13 Avatar answered Oct 18 '22 13:10

zsmb13