Skip to content

Instantly share code, notes, and snippets.

@pakoito
Last active Feb 13, 2022
Embed
What would you like to do?
ContextN
inline fun <A, T> withN(t: T, f: context(T) () -> A) =
with(t) { f() }
inline fun <A, T, U> withN(t: T, u: U, f: context(T, U) () -> A) =
with(t) { with(u) { f() } }
inline fun <A, T, U, V> withN(t: T, u: U, v: V, f: context(T, U, V) () -> A) =
with(t) { with(u) { with(v) { f() } } }
inline fun <A, T, U, V, W> withN(t: T, u: U, v: V, w: W, f: context(T, U, V, W) () -> A) =
with(t) { with(u) { with(v) { with(w) { f() } } } }
inline fun <A, B, T> A.runN(t: T, f: context(T) A.() -> B) =
withN(t) { f() }
inline fun <A, B, T, U> A.runN(t: T, u: U, f: context(T, U) A.() -> B) =
withN(t, u) { f() }
inline fun <A, B, T, U, V> A.runN(t: T, u: U, v: V, f: context(T, U, V) A.() -> B) =
withN(t, u, v) { f() }
inline fun <A, B, T, U, V, W> A.runN(t: T, u: U, v: V, w: W, f: context(T, U, V, W) A.() -> B) =
withN(t, u, v, w) { f() }
@pakoito
Copy link
Author

pakoito commented Feb 13, 2022

context(Service1, Service2, Service3, Service4)
suspend fun someFun(s: String) = TODO()

withN(serv1, serv2, serv3, serv4) {
  someFun("1")
}

"1".runN(serv1, serv2, serv3, serv4, ::someFun)

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