Feature request: support sum type in macwire.
As the code show in gitter
trait DesuConfigModel:
val configIO: IO[DesuConfig] = ???
end DesuConfigModel
trait AppConfig(config: DesuConfig):
end AppConfig
trait DoobieDB(config: DesuConfig):
private val dsConfigIO = IO(config.mysqlDesuQuillDB.dataSource)
val transactor: Resource[IO, HikariTransactor[IO]] = ???
end DoobieDB
trait FileFinder(appConfig: AppConfig, xa: Transactor[IO]):
// code
end FileFinder
trait AppRoutes(fileFinder: FileFinder, appConfig: AppConfig):
// code
end AppRoutes
val configModel = wire[DesuConfigModel]
val appRoutes: Resource[IO, AppRoutes] = for
desuConfig <- Resource.eval(configModel.configIO)
appConfig = wire[AppConfig]
doobieDB = wire[DoobieDB]
xa <- doobieDB.transactor
yield
val fileFinder = wire[FileFinder]
wire[AppRoutes]
Macwire can support wire in F[_] like distage. Since implicit value and constructor value is fetched by type. You can declare a implicit value like
implicit val implicitVarName = constructorVarName
Here request a feature that support sum type in macwire like zio.ZEnvironment. Then we can support something like distage's Module include(simple version) in Scala2 and Scala3.(distage doc)
Here is the code also in gitter
class DesuConfigModelImpl extends DesuConfigModel
class AppConfigImpl(using DesuConfig) extends AppConfig(summon)
class DoobieDBImpl(using DesuConfig) extends DoobieDB(summon)
class FileFinderImpl(using AppConfig, Transactor[IO]) extends FileFinder(summon, summon)
class AppRoutesImpl(using FileFinder, AppConfig) extends AppRoutes(summon, summon)
import zio.{IO as _, *}
object MainAppInjected:
type ProjectEnvModule1 = DesuConfig & AppConfig & Transactor[IO]
given [T: Tag, S <: T](using ZEnvironment[S]): T = summon[ZEnvironment[S]].get
val envResource: Resource[IO, ZEnvironment[ProjectEnvModule1]] = for
given DesuConfig <- Resource.eval((new DesuConfigModelImpl).configIO)
given AppConfig = new AppConfigImpl
doobieDB = new DoobieDBImpl
given Transactor[IO] <- doobieDB.transactor
yield ZEnvironment(implicitly[DesuConfig], implicitly[Transactor[IO]], implicitly[AppConfig])
val appRoutes: Resource[IO, AppRoutes] = for
given ZEnvironment[ProjectEnvModule1] <- envResource // distage include(simple version)
yield
given FileFinder = new FileFinderImpl
new AppRoutesImpl
end MainAppInjected
It circuitously performs the macwire function I imagined use Scala3 and zio.ZEnvironment.
And I find that macwire perhaps can work fine with cats-effect-cps
Here's the new version with inject part(also use other way to implement, thanks for the powerful expressive ability in Scala3)
object MainAppInjected:
object module1 extends InjectedModule1
import module1.{env as env1, Env as Env1}
val appRoutes: Resource[IO, AppRoutes] = async[Resource[IO, *]] {
given ZEnvironment[Env1] = env1.await
given FileService = new FileServiceImpl
given FileFinder = new FileFinderImpl
new AppRoutesImpl
}
end MainAppInjected
trait InjectedModule1:
type Env = DesuConfig & AppConfig & Transactor[IO]
val env: Resource[IO, ZEnvironment[Env]] = async[Resource[IO, *]] {
val configModel = new DesuConfigModelImpl
given DesuConfig = Resource.eval(configModel.configIO).await
given AppConfig = new AppConfigImpl
val doobieDB = new DoobieDBImpl
given Transactor[IO] = doobieDB.transactor.await
ZEnvironment(implicitly[DesuConfig], implicitly[Transactor[IO]], implicitly[AppConfig])
}
end InjectedModule1
given [ModelTag: Tag, S <: ModelTag](using ZEnvironment[S]): ModelTag = summon[ZEnvironment[S]].get
And the cps lib seems that can work with zio and scala future.