Creating instances
Two ways to create objects without runtime reflection: a macro that calls a case class's constructor, and a factory bound to a descriptor.
Case classes
createInstance
The arguments are passed to the primary constructor in order.
- ReflectMacros.createInstance[Person]("Ada", 37)
- Person(Ada,37)
scala
val person = ReflectMacros.createInstance[Person]("Ada", 37)
Factories
Bound to a descriptor
bindFactory attaches a function that creates an instance. createInstance on the descriptor, the registry or a class loader calls it.
- query.createInstance()
- Some(Query(select * from dual))
- reflect[Query].createInstance()
- None
scala
val query = ReflectMacros.reflect[Query].bindFactory(() => Query("select * from dual"))
query.createInstance() // Some(Query(select * from dual))