Answer the question
In order to leave comments, you need to log in
How to get a class from a type passed as a parameter to the constructor of another class?
Let's say we have a case class and we want to read its fields.
We do so
case class Product (name: String, price: Int)
val fields = classOf[Product].getDeclaredFields
class SqlModels[T] {
// Здесь охота прочитать поля из T
}
val sqlInstance = new SqlModels[Product]
Answer the question
In order to leave comments, you need to log in
You need a ClassTag
import scala.reflect._
case class Product(name: String, price: Int)
class SqlModels[T : ClassTag] {
def fields = classTag[T].runtimeClass.getDeclaredFields
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question