Funzo
Funzo

Reputation: 1290

In Scala is it possible to create class object of class which takes generic

In the code below at runtime the value will just be an instance of class list. Is it possible to create an instance of class List[T] using typetags or classtags?

  private def method[T <: Product](clazz: Class[T]) = {
    val value: Class[List[T]] = classOf[List[T]]
  }

Upvotes: 0

Views: 64

Answers (1)

Evgeny Lyutikov
Evgeny Lyutikov

Reputation: 56

import scala.reflect.ClassTag

def create[A: ClassTag](): List[A] = List.empty[A]
create[Int]()

Upvotes: 1

Related Questions