CLOVIS
03/15/2023, 10:45 AMinterface A
interface B
context(A, B)
fun foo() { … }
can I call it like this?
class Context : A, B
with(Context()) {
foo()
}
Said otherwise, can a single object satisfy multiple context receivers?pakoito
03/15/2023, 10:47 AMCLOVIS
03/15/2023, 10:48 AMpakoito
03/15/2023, 10:49 AMsimon.vergauwen
03/15/2023, 10:50 AMCLOVIS
03/15/2023, 10:51 AMcarbaj0
03/15/2023, 10:52 AMpakoito
03/15/2023, 11:03 AMCLOVIS
03/15/2023, 11:04 AMYoussef Shoaib [MOD]
03/15/2023, 11:22 AMFir2IrComponents
as a context receiver for utility functions, and then in the classes that contain those components, they're defined like:
class CallAndReferenceGenerator(
private val components: Fir2IrComponents,
private val visitor: Fir2IrVisitor,
private val conversionScope: Fir2IrConversionScope
) : Fir2IrComponents by components
I think there was a time where they were doing that pattern with multiple context-like classes and not just Fir2IrComponents
, but searching the codebase now it seems that they have everything they need within that class and hence its the only context-like class I could find that's used with that delegation patternFir2IrComponents
was introduced in March 2020, way before context receivers were in the language, so it's likely that the usage of delegation there is an artifact of that time and there just hasn't been enough of a reason to change it to using a context
receiver on the class