0

我正在学习 twitter 的 RPC 异步系统,使用过滤器和服务处理逻辑任务很重要。我已经阅读了https://twitter.github.io/finagle/guide/ServicesAndFilters.html的论文并尝试编写一个简单的代码来制作真的,我知道。但是有一些问题:

import com.twitter.util.Future

trait Service[Req, Rep] extends (Req => Future[Rep])

abstract class Filter[-ReqIn, +RepOut, +ReqOut, -RepIn]
  extends ((ReqIn, Service[ReqOut, RepIn]) => Future[RepOut])

trait SimpleFilter[Req, Rep] extends Filter[Req, Rep, Req, Rep]

class DoubleFilter(times: Int) extends SimpleFilter[(Int,Int), Int] {
  def apply(request: (Int,Int), service: Service[(Int,Int), Int]): Future[Int] = {
    service((request._1 * times, request._2 * times))
  }
}

class AddFilter(count: Int) extends SimpleFilter[(Int, Int), Int] {
  def apply(request: (Int, Int), service: Service[(Int, Int),Int]): Future[Int] = {
    service((request._1 + count, request._2 + count))
  }
}

object Test extends App {
  val doubleFilter = new DoubleFilter(2)
  val addFilter = new AddFilter(10)

  //req is tuple, rep is added count of int
  val addELemsService = new Service[(Int,Int), Int] {
    def apply(req: (Int,Int)) = {
      Future {
        req._1 + req._2
      }
    }
  }

  val serviceWithDoble = doubleFilter andThen addELemsService

  val doubleWithAddFilter = doubleFilter andThen addFilter
}

结果有两个编译错误:value 和Then 不是DoubleFilter 的成员!
我想念什么明白吗?我直认为医生忽略了一些东西,但我误导了。

4

1 回答 1

0

我认为您正在导入错误的特征/类。我尝试使用正确的导入编译以下代码,并且编译时没有错误。

import com.twitter.finagle.{Service, Filter}
import com.twitter.util.Future


object Test extends App {

  trait SimpleFilter[Req, Rep] extends Filter[Req, Rep, Req, Rep]

  class DoubleFilter(times: Int) extends SimpleFilter[(Int, Int), Int] {
    def apply(request: (Int, Int), service: Service[(Int, Int), Int]): Future[Int] = {
      service((request._1 * times, request._2 * times))
    }
  }

  class AddFilter(count: Int) extends SimpleFilter[(Int, Int), Int] {
    def apply(request: (Int, Int), service: Service[(Int, Int), Int]): Future[Int] = {
      service((request._1 + count, request._2 + count))
    }
  }

  val doubleFilter = new DoubleFilter(2)
  val addFilter = new AddFilter(10)
  //req is tuple, rep is added count of int
  val addELemsService = new Service[(Int, Int), Int] {
    def apply(req: (Int, Int)) = {
      Future {
        req._1 + req._2
      }
    }
  }
  val serviceWithDoble = doubleFilter andThen addELemsService
  val doubleWithAddFilter = doubleFilter andThen addFilter

}
于 2016-04-06T12:20:35.377 回答