There he is:
import org.hibernate.Session class DataLoader(s: Session) { def load[A](id: Long)(implicit m: Manifest[A]): A = { return s.load(m.erasure, id).asInstanceOf[A]; } }
EDIT . Or, to ensure that any casting error - as a result of sleep mode that returns the wrong object - will happen inside load , for example:
import org.hibernate.Session class DataLoader(s: Session) { def load[A](id: Long)(implicit m: Manifest[A]): A = { return m.erasure.asInstanceOf[Class[A]].cast(s.load(m.erasure, id)); } }
In Scala 2.8, you can also write it like this:
import org.hibernate.Session class DataLoader(s: Session) { def load[A : Manifest](id: Long): A = { return s.load(manifest[A].erasure, id).asInstanceOf[A]; } }
You can combine it with an implicit session as suggested by Chris :
def load[A](id: Long)(implicit m: Manifest[A], s: org.hibernate.Session): A = { return s.load(m.erasure, id).asInstanceOf[A]; }
Note that you cannot combine contextual notation ( A : Manifest ) with additional implicit parameters.
source share