I have a factory that should return an implementation depending on the name.
val moduleMap = Map(Modules.moduleName -> new ModuleImpl)
def getModule(moduleName: String): Module =
moduleMap.get(moduleName) match {
case Some(m) => m
case _ =>
throw new ModuleNotFoundException(
s"$moduleName - Module could not be found.")
}
In order for each call to the "getModule" method not to create an instance, there is a map in which all the modules must be initialized in bootstrap class. I would like to get rid of the need to do this manually(also all classes have a distinctive feature).
List of options that came to my mind:
- Reflection(we can use Scala Reflection API or any thrid-party library)
- Automated process.
- Need to initialize immediately at startup.
- Reflection is a pain.
- Metaprogramming(ScalaMeta) + Reflection
- Macros only change the code, the execution happens later.
Can we move initialization process to compile time?
I know that compiler can optimize and replace code, next fragment before compilation
val a = 5 + 5
after compilation compiler change that piece to 10, can we use some directives or another tools to evaluate and execute some code at compile time and use only final value?
Aucun commentaire:
Enregistrer un commentaire