在参数化抽象类级别(Scala)中调用隐式类

时间:2015-01-21 17:20:39

标签: scala implicit parameterized

我有一个问题,如下所示:

//project entities

abstract class AbstractEntity
class EntityA extends AbstractEntity

// implicit converters
trait Converter[V] {def convert (entity: V): V}
object Converters{

  implicit object ConvertEntityA extends Converter[EntityA]{
  def convert = ???//do something
   .....
  }
}

//Use this class to perform convert operation
object ConvertUtils{
  def an[V](entity: V)(implicit converter: Converter[V]) =      
  converter.convert(entity)
  ....
}

//utility classes
abstract class AbstractEntityUtil{
  val entityClass: Class[T] = ... gets it by reflection
  val convertedEntity = Convert an entityClass.newInstance()  
}
class EntityAUtil extends AbstractEntityUtil{
   def printIt = print(convertedEntity)

}

我想从EntityAUtil调用print,它会触发一个  转换ConvertEntityA,但得到明显的错误:

错误:(38,31)找不到参数转换器的隐含值:packageName.Converter [V]      “val entity:T = Convert a(entityClass.newInstance())”                                ^

我设法只在从ConvertEntityA调用convertEntity时才能使它工作, 我可以按照我想要的方式使它工作吗?

0 个答案:

没有答案