https://kotlinlang.org logo
#compose
Title
# compose
b

Bryan Lee

12/17/2019, 7:33 PM
Can someone explain to me what this does?
Copy code
private fun WebView.setRef(ref: (WebView) -> Unit) {
    ref(this)
}
Taken from ui/ui-android-view/src/main/java/androidx/ui/androidview/WebComponent.kt
l

Luca Nicoletti

12/17/2019, 7:40 PM
Simply runs the
ref
function it accept as a parameter providing
this
as receiver of that function 🤔
b

Bryan Lee

12/17/2019, 7:48 PM
mm I see. Unfamiliar with how that works. I'll look into that, thanks
z

Zach Klippenstein (he/him) [MOD]

12/17/2019, 8:38 PM
I think the reason for this is that to “render” a legacy
View
in a composable, you invoke it as a function – you don’t instantiate it, so you never actually have a reference to the
View
object. This pattern gets you around that.
1
This message+thread has some more details about the general integration story right now: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1576264533012000?thread_ts=1576262311.008800&cid=CJLTWPH7S
l

Leland Richardson [G]

12/17/2019, 8:54 PM
yeah…. compose will use properties and setter functions on views and interpret them as parameters. The interesting bit is it will also use extension properties/setters if they are in scope. So the
WebView.setRef
function above gets interpreted as a
ref
parameter in the composable webview call. It’s a bit weird, which is why we haven’t talked a ton about it yet, and some of it still might change
😮 1
b

Bryan Lee

12/17/2019, 10:07 PM
gotcha, thanks for the help
z

Zach Klippenstein (he/him) [MOD]

12/18/2019, 6:02 PM
Oh, that extension property support is super useful.
2 Views