0

I have the following...

trait SourceTrait[T <: Connector] extends BeforeAndAfterAll{
  ...
  override def beforeAll(): Unit = {
    super.beforeAll()
    ds = new T
    connector = ds.asInstanceOf[Connector]
  }
}

The idea is that now I can create custom traits for a variety of type like this with SourceTrait[Thing]

The problem is I get class type required but T found when I try to compile this code.

Is there a way in a Scala trait I can create a new instance of a Generic Class?

This needs to work specifically for a Trait.

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • Possible duplicate of [Class type required but T found](https://stackoverflow.com/questions/20591957/class-type-required-but-t-found) – Ziyang Liu Sep 18 '17 at 14:51
  • Not a dupe, that deals with adding a factory to the constructor of a class which cannot be done (as far as I know) with a trait. There is a big difference in java between a trait and a class. – Jackie Sep 18 '17 at 14:57
  • 3
    Yup, cannot be done. If you can't make it a class, you'll have to define an abstract factory to be overwritten by implementations `def newT: T` – Dima Sep 18 '17 at 15:00

0 Answers0