Skip to content

Instantly share code, notes, and snippets.

@httpdispatch
Last active March 26, 2024 10:36
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/04e4055fb12f9c920c574517855e66c5 to your computer and use it in GitHub Desktop.
Save httpdispatch/04e4055fb12f9c920c574517855e66c5 to your computer and use it in GitHub Desktop.
class Context
interface Module1 {
val dep1: () -> Int
val dep2: String
interface 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
interface 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
interface 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
interface 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(
override val context: Context,
private val self: () -> AppComponent,
) :
Module1.Deps,
Module1 by Module1(self),
Module2.Deps,
Module2 by Module2(self),
FeatureModule2.Deps,
FeatureModule2.Holder by FeatureModule2.Holder(self),
FeatureModule1.Deps,
FeatureModule1.Holder by FeatureModule1.Holder(self) {
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