https://kotlinlang.org logo
#stdlib
Title
m

Marco Pierucci

07/31/2023, 2:16 PM
Context receiver doubt 🧵
Copy code
context(FieldSetInfo.Component, BeFormFieldComposer)
fun camera() : TrailingIcon {
    return TrailingIcon(
        type = TrailingIcon.Type.CAMERA,
        onClicked = {
            formNavigator.launchCamera(this)
        }
    )
}
If I do this I get
this is not defined in this context
however calling
formNavigator.launchCamera(copy())
for example, works
whats the right way of calling the
FieldSetInfo.Component
context here?
y

Youssef Shoaib [MOD]

07/31/2023, 2:21 PM
it's gonna be
this@Something
, although I'm not sure whether the something would be
Component
or
FieldSetInfo
m

Marco Pierucci

07/31/2023, 2:21 PM
Copy code
this@camera
is of the type I want as per the IDE autocomplete but I get the same error message
y

Youssef Shoaib [MOD]

07/31/2023, 2:22 PM
Alternatively, you can use this nice function:
Copy code
context(A) fun <A> given(): A = this@A
like this:
given<FieldSetInfo.Component>()
🙌 1
❤️ 1
IDE autocomplete is wrong in this case. Intellij doesn't support context receivers well just yet
m

Marco Pierucci

07/31/2023, 2:22 PM
ah good to know
So this@Component did it thanks! I do think your proposal for the "summoning" function sounds quite cool
2 Views