Ruckus
04/15/2022, 3:26 PMfun generate(selection: Selection, values: Values) { ... }
context(Scope) fun generate(selection: Selection, values: Values) =
generate(this@Scope.selection refine selection, values)
^ this is a recursive call, but I would like to call the other (non context(Scope)) generate functionYoussef Shoaib [MOD]
04/16/2022, 3:03 PMfun generate(selection: Selection, values: Values) { ... }
private fun _generate(selection: Selection, values: Values) = generate(selection, values)
context(Scope) fun generate(selection: Selection, values: Values) =
_generate(this@Scope.selection refine selection, values)Youssef Shoaib [MOD]
04/16/2022, 3:19 PMfun generate(selection: Selection, values: Values) {}
context(Scope) fun generate(selection: Selection, values: Values) {
val generate: (Selection, Values) -> Unit = ::generate
generate(this@Scope.selection refine selection, values)
}
Interestingly, something like KT-44530 will remove that overhead (which, shameless plug, I've been working on a compiler plugin for, but I haven't yet updated to 1.6.20)Youssef Shoaib [MOD]
04/16/2022, 3:23 PMfun generate(selection: Selection, values: Values) {}
context(Scope) fun generate(selection: Selection, values: Values): Unit =
invokeContextless(::generate, /*this@Scope,*/ this@Scope.selection refine selection, values)
inline fun <A, B, R> invokeContextless(action: (A, B) -> R, a: A, b: B): R = action(a, b)
inline fun <A, B, C, R> invokeContextless(action: (A, B, C) -> R, a: A, b: B, c: C): R = action(a, b, c)
The compiler seems to understand which function you intend by that function reference based on the amount of parameters that you pass to invokeContextless, try that out yourself by uncommenting the this@Scope .
Personally, this is a well-balanced solution in that it's concise enough but it doesn't sacrifice performance. I would've preferred however if instead the function reference solution would've had no overhead, or if we could get a way to remove receivers from a scopeRuckus
04/17/2022, 4:07 PM