문제

I have a trait EsperClassification defined as follows:

trait EsperClassification {
   import com.gensler.scalavro.util.Union
   import scala.reflect.runtime.{currentMirror => m}
   type EsperEvents

   // what I want to do is this, but this fails:
   val u = new Union[EsperEvents]
   // so that I can do this:
   u.memberTypes() foreach { t=> registerType(t.typeSymbol.Name,m.runtimeClass(t))}

   private def registerType(name:String, clz: Class[_ <: Any]) = ...
}

This trait is used as follows:

class EsperEventBus extends ActorEVentBus with EsperClassification {
  type EsperEvents = union[Buy] #or [Sell]

  … more stuff
}

this fails at the instantiation of Union with the scala compiler complaining about missing TypeTags.

However, if I instantiate the Union in the concrete EsperEventBus it works fine:

trait EsperClassification {
  import com.gensler.scalavro.util.Union
  import scala.reflect.runtime.{currentMirror => m}
  type EsperEvents
  def esperEventsUnion: Union[EsperEVents] // abstract!

  // this works:
  esperEventsUnion.memberTypes() foreach { 
      t=> registerType(t.typeSymbol.Name,m.runtimeClass(t))}

  private def registerType(name:String, clz: Class[_ <: Any]) = ...
}

trait EsperEventBus extends ActorEventBus with EsperClassification {
  type EsperEvents = union[Buy] #or [Sell]

  def esperEventsUnion = new Union[EsperEvents] 
}

What I would like to know is if and how I can instantiate the Union in the trait itself?

Thanks,

Frank

도움이 되었습니까?

해결책

Unfortunately, Union requires resolution of an implicit TypeTag for the underlying disjunction of types. The only workaround is as you've done: delay instantiating the Union until such evidence is available to scalac.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top