elect
10/25/2022, 6:58 AMclass Vec2(val x: Float, val y: Float) {
inline fun plus(s: Float, res: (resX: Float, resY: Float) -> Unit) {
kotlin.contracts.contract { callsInPlace(res, kotlin.contracts.InvocationKind.EXACTLY_ONCE) }
res(x + s, y + s)
}
}
This code can then be called as
val a = Vec2(0, 1)
val b: Float; val c: Float
a.plus(3) { x, y -> b = x; c = y }
it's useful in hot loops (in 3d real time graphics) or, in Louis' example in Android, where you want to save any allocation you can
Now, It'd be nice if Kotlin may automatically destructurize a lambda placed at the last place in a function signature without any allocations. That is something like:
val a = Vec2(0, 1)
val (b, c) = a.plus(3) { x, y }
while underneath the compiler would automatically apply the code as shown in the first example
Another nice advantage, which would come for "free", it's essentially using an argument parameter as a return one, overcoming the JVM limitation of not considering the return type as part of the function signatureDominaezzz
10/25/2022, 11:00 AMelect
10/25/2022, 12:58 PMelect
10/26/2022, 11:48 AM