Skip to content

Instantly share code, notes, and snippets.

@httpdispatch
Created March 26, 2024 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save httpdispatch/d126934db965afcf8954d52a445995f2 to your computer and use it in GitHub Desktop.
Save httpdispatch/d126934db965afcf8954d52a445995f2 to your computer and use it in GitHub Desktop.
class Context
interface Module1 {
val dep1: () -> Int
val dep2: String
data class Deps(
val context: Context,
)
private class Impl(val deps: () -> Deps) : Module1 {
override val dep1: () -> Int = { 42 }
override val dep2: String
get() = "M1Dep2 Impl " + deps().context
}
companion object : (() -> Deps) -> Module1 by { Impl(it) }
}
interface Module2 {
val dep3: String
data class Deps(
val dep1: () -> Int,
val context: Context,
)
private class Impl(val deps: () -> Deps) : Module2 {
override val dep3: String
get() = deps().run {
"M2" + dep1() + context + "m2"
}
}
companion object : (() -> Deps) -> Module2 by { Impl(it) }
}
interface FeatureModule1 {
val f1dep: String
data class Deps(
val dep2: String,
val dep3: String,
)
interface Holder {
val featureModule1: FeatureModule1
private class Impl(val deps: () -> Deps) : Holder {
override val featureModule1: FeatureModule1 get() = FeatureModule1(deps)
}
companion object : (() -> Deps) -> Holder by { Impl(it) }
}
companion object : (() -> Deps) -> FeatureModule1 by { Impl(it) } {
private class Impl(val deps: () -> Deps) : FeatureModule1 {
override val f1dep: String
get() = deps().run {
"F1$dep2$dep3"
}
}
}
}
interface FeatureModule2 {
val f2dep: String
data class Deps(
val dep2: String,
val dep3: String,
)
interface Holder {
val featureModule2: FeatureModule2
private class Impl(val deps: () -> Deps) : Holder {
override val featureModule2: FeatureModule2 get() = FeatureModule2(deps)
}
companion object : (() -> Deps) -> Holder by { Impl(it) }
}
companion object : (() -> Deps) -> FeatureModule2 by { Impl(it) } {
private class Impl(val deps: () -> Deps) : FeatureModule2 {
override val f2dep: String
get() = deps().run {
"F2$dep2$dep3"
}
}
}
}
class AppComponent private constructor(
val context: Context,
private val self: () -> AppComponent,
) :
Module1 by Module1({ Module1.Deps(context = context) }),
Module2 by Module2({
Module2.Deps(
dep1 = self().dep1,
context = context
)
}),
FeatureModule1.Holder by FeatureModule1.Holder({
FeatureModule1.Deps(
dep2 = self().dep2,
dep3 = self().dep3
)
}),
FeatureModule2.Holder by FeatureModule2.Holder({
FeatureModule2.Deps(
dep2 = self().dep2,
dep3 = self().dep3
)
}) {
companion object {
operator fun invoke(context: Context): AppComponent {
var component: AppComponent? = null
val self = { requireNotNull(component) }
return AppComponent(context = context, self = self)
.also { component = it }
}
}
}
val c = AppComponent(context = Context())
println("Start")
println(c.dep1())
println(c.dep2)
println(c.dep3)
println(c.featureModule1.f1dep)
println(c.featureModule2.f2dep)
println("End")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment