从子类隐含

时间:2015-09-30 18:56:26

标签: scala implicit spray

我正在尝试使用implicits实现模板方法模式。 A类是具有2个扩展的顶级类。

定义模板模式的A类,并有一些方法doSomething()。此方法内部使用了几个含义。这个类是抽象的。

B类和C类正在扩展A类,在范围内提供隐含,实现其他抽象方法并且不再抽象。

示例(使用喷雾管道):

A:

abstract class A[T <: Any : Manifest] {
    .... 
    abstract def method: PartialFunction[T,Unit] //not interesting

    def doSomething() {
         //method using implicit parser HttpResponse -> T
         val responseFuture: Future[T] = pipeline(Get(url))
         responseFuture.onSuccess(method)
    }
}


B:

import CaseClassB._ //importing implicits that transfer HttpResponse -> CaseClassB
class B extends A[CaseClassB] {
    def method: PartialFunction[CaseClassB,Unit] = {//not interesting}
}

此设置不起作用(由于“无法找到证据参数的隐含值”,因此编译A类失败)。有没有更好的方法来解决这个问题,而不需要像A类中的“隐式抽象def”那样需要重写?我不喜欢这个的原因是B级和C级使用进口提供隐含。

0 个答案:

没有答案
相关问题