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.
how do I reflect on Scalavro union types in a base trait with abstract type variable?
-
16-10-2022 - |
質問
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
解決
所属していません StackOverflow