Question

I have a trait that is implementing a method defined in a supertrait with no arguments. My implementation of the method requires a type parameter that is defined on the trait, but I can't add an (implicit ct:ClassTag[Foo]) because that would change the method's argument list and it no longer counts as implementing the method on the supertrait. Concretely:

import scala.reflect.ClassTag

trait AbstractFoo
trait Performance
trait Performer {def perform:Performance}

class FooPerformer[Foo <: AbstractFoo](implicit ct:ClassTag[Foo]) extends Performance {
  // do things
}        

trait Fooer[Foo <: AbstractFoo] extends Performer {
  def perform = new FooPerformer[Foo]
}

I need the implicit ClassTag on FooPerformer to deal with an erasure in the implementation. As it stands, I get a compile error for a missing ClassTag on perform in Fooer, but if I change it to:

trait Fooer[Foo <: AbstractFoo] extends Performer {
  def perform(implicit ct:ClassTag[Foo]) = new FooPerformer[Foo]
}

It no longer implements perform on Performer.

Was it helpful?

Solution

@wingedsubmariner meant:

scala> class Fooer[Foo <: AbstractFoo : ClassTag] extends Performer {
     |   def perform = new FooPerformer[Foo]
     | }
defined class Fooer

Also,

scala> trait Fooer[Foo <: AbstractFoo] extends Performer {
     |   implicit protected def tag: ClassTag[Foo]
     |   def perform = new FooPerformer[Foo]
     | }
defined trait Fooer

scala> class Foob extends Fooer[AbstractFoo] {
     | protected def tag = reflect.classTag[AbstractFoo]
     | }
defined class Foob
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top