I have a simple trait
trait BusinessObject {}
and a simple type-class
trait Printer[T<:BusinessObject] { def print(instance:T): Unit }
In my code base I have a few hundred implementations of BusinessObject
. Some are direct implementers, some implement sub-traits of BusinessObject
, and some add various mixin traits using with
. I have around 10 different special implementations of Printer
(defined on the various sub-traits and mixins), and a low-priority generic fallback instance for any other BusinessObject
and it works a charm.
I need to document all the implementations of BusinessObject
in the code base, so I have used Scala's reflection mechanism to enumerate these, and now want to apply a Printer on each. The method signature of the reflection mechanism is
def enumerateBOs: Traversable[BusinessObject]
It returns one instance of each BusinessObject
implementation. My problem is that at runtime there seems to be no way to get the right (specific) Printer for each object in this traversable.
I have tried summoning using .type
like this:
enumerateBOs.head match { case bo => Printer[bo.type].print(bo) }
but I get the generic fallback Printer
for every element.
Is there some way to do what I want to do? Or, if implicits really only are available at compile time, some way to list all implementers of BusinessObject
at compile time?
Aucun commentaire:
Enregistrer un commentaire