Skip to content

Instantly share code, notes, and snippets.

@Takhion
Created July 8, 2018 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Takhion/669d7873a0425161304cee729ad08401 to your computer and use it in GitHub Desktop.
Save Takhion/669d7873a0425161304cee729ad08401 to your computer and use it in GitHub Desktop.
Multiple receivers in Kotlin
val sample: (X, Y, Z) -> Int = multiReceiver { { { x + y + z } } }
fun <A, B, C, R> multiReceiver(f: A.() -> B.() -> C.() -> R) = { a: A, b: B, c: C -> f(a)(b)(c) }
class X(val x: Int)
class Y(val y: Int)
class Z(val z: Int)
@bkahlert
Copy link

It would have saved me hours if someone told me, how to actually call sample now.
Answer is: sample(X(1), Y(2), Z(3))
I hope that helps future desperate souls looking for ways to accept multiple receivers.

Everyone trying to find out what sample would look like without the multiReceiver helper:

val sample: X.() -> Y.() -> Z.() -> Int =  { { { x + y + z } } }

class X(val x: Int)
class Y(val y: Int)
class Z(val z: Int)

val sampleResult = sample(X(1))(Y(2))(Z(3))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment