我有一个简单的服务定义
trait Service[-Req, +Rep] extends (Req => Future[Rep]) {
def apply(request: Req): Future[Rep]
}
以及如何链接服务的方法:
implicit class ServiceOps1[Req, RepIn](service: Service[Req, RepIn]) {
def -->[RepOut](next: Service[RepIn, RepOut]): Service[Req, RepOut] =
(req: Req) => service(req) flatMap next
}
我想将我的所有服务(假设它们可以组合)放入HList
并从HList
服务组合中构建。
这是我的Resolver
trait Resolver[L <: HList, In] {
type Out
def apply(l: L): Service[In, Out]
}
object Resolver {
def apply[L <: HList, In](implicit resolver: Resolver[L, In]): Aux[L, In, resolver.Out] = resolver
type Aux[L <: HList, In, Out0] = Resolver[L, In] { type Out = Out0 }
implicit def hsingleResolver[I, O, S <: Service[I, O]]: Aux[S :: HNil, I, O] =
new Resolver[S :: HNil, I] {
type Out = O
def apply(l : S :: HNil): Service[I, Out] = l.head
}
implicit def hlistResolver[I, O, S <: Service[I, O], T <: HList](implicit res : Resolver[T, O]): Aux[S :: T, I, res.Out] =
new Resolver[S :: T, I] {
type Out = res.Out
def apply(l: S :: T): Service[I, res.Out] = l.head --> res(l.tail)
}
}
我有服务
object S extends Service[Int, String] {
def apply(request: Int): Future[String] = Future successful request.toString
}
当我尝试解决简单链时
implicitly[Resolver[S.type :: HNil, Int]].apply(S :: HNil)
我得到一个隐含的未找到错误。