Class type required but T found

前端 未结 3 2007
误落风尘
误落风尘 2020-12-10 15:16

I can\'t create an actor for some reason (here is a simple version of my class hierarchy):

abstract class Class1[T <: Class2[_]: ClassTag] extends Actor {         


        
3条回答
  •  半阙折子戏
    2020-12-10 15:24

    One approach I have used involves creating an 'instantiator' trait which can be used to create instances of types for which an implicit instantiator exists:

    trait Instantiator[+A] {
      def apply(): A
    }
    
    object Instantiator {
      def apply[A](create: => A): Instantiator[A] = new Instantiator[A] {
        def apply(): A = create
      }
    }
    
    class Foo() { ... }
    
    object Foo {
      implicit val instantiator: Instantiator[Foo] = Instantiator { new Foo() }
    }
    
    // def someMethod[A]()(implicit instantiator: Instantiator[A]): A = {
    def someMethod[A : Instantiator](): A = {
      val a = implicitly[Instantiator[A]].apply()
      ...
    }
    
    someMethod[Foo]()
    

提交回复
热议问题