I've got a pseudo-generic class factory that I'm using for DAO purposes. The operational model is pretty straightforward:
1) the factory is instantiated with knowledge of a specific DAO-object "T"ype
2) it then creates an internal instance of T
3) it should call a trait-contracted function of T
4) finally, return the instance of T (assuming all goes well)
It looks like this:
class DAOFactory[T](implicit m: Manifest[T]) {
def obj = m.runtimeClass.asInstanceOf[Class[T]]
def doFoo(): Option[Any] = {
val dao = obj.asInstanceOf[DAO] // (A) this line will crash
println(obj.toString()) // some.object.called.DAOTodo
// val bar = dao.doSomethingSmart(now) <--- ALL DAO-derived classes implement this function
return Some(obj) // error-catching excluded for brevity
}
}
It is used like this:
val f = new DAOFactory[DAOTodo]
val shinyNewObject = f.doFoo() // never gets here.
Where 'DAOTodo' is, in fact, a class that implements the DAO trait:
class DAOTodo extends DAO {
def doSomethingSmart(when: Whenever) = {...}
}
Q: what needs to be done at point "A" to use DAO as the interface to the "doSomethingSmart" function of "obj"?
Thanks in advance.
ANSWER: Aside from the code modification as outlined in the answer below, this was not running as predicted due to the fact that the lineage for the classes being created (within the factory) had a primary constructor that was not being fulfilled. Creating an additional zero-param constructor resolved the issue.