加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 在akka http路由列表上调用reduce会产生编译错误(参数

发布时间:2020-12-16 18:32:03 所属栏目:安全 来源:网络整理
导读:鉴于以下路线 val route1: PathMatcher[Unit] = PathMatcher("app")val route2: PathMatcher1[String] = PathMatchers.Segmentval route3: PathMatcher[Unit] = PathMatcher("lastSegment") 我可以轻松定义 val resultingRoute: PathMatcher[Tuple1[String]]
鉴于以下路线

val route1: PathMatcher[Unit] = PathMatcher("app")
val route2: PathMatcher1[String] = PathMatchers.Segment
val route3: PathMatcher[Unit] = PathMatcher("lastSegment")

我可以轻松定义

val resultingRoute: PathMatcher[Tuple1[String]] = route1 / route2 / route3

获得预期的类型(PathMatcher [Tuple [String]]).

但是以编程方式创建路线

val routeDef = List(route1,route2,route3)
val resultingRoute = routeDef.reduce((a,b) => a / b)

不会编译,给我

could not find implicit value for parameter join: akka.http.scaladsl.server.util.TupleOps.Join[_1,_1]

此外,推断出的resultRoute类型是

PathMatcher[_ >: Unit with Tuple1[String] with join.Out]

我真的很感激有任何提示给我一些迹象,说明我在这里做错了什么,或者如何解决这个问题.

为了完整性,这是我的导入:

import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{PathMatcher,_}

非常感谢!

解决方法

您的问题是您的routeDef列表实际上是异构的,编译器将其类型推断为List [PathMatcher [_>:Tuple1 [String] with Unit]].

鉴于此,(a:PathMatcher [L])./(b:PathMatcher [R])方法需要隐式地需要TupleOps.Join [L,R]:akka.http.scaladsl.server.PathMatcher.无法从routeDef列表中的PathMatchers类型推断出它.

如果您愿意使用shapeless,那么您可以轻松地处理异构列表(在此上下文中称为HLists):

import shapeless._
val routeDef = route1 :: route2 :: route3 :: HNil
object join extends Poly {
  implicit def casePathMatcher[A,B](
    implicit t: akka.http.scaladsl.server.util.TupleOps.Join[A,B]
  ) = use((a: PathMatcher[A],b: PathMatcher[B]) => a/b)
}
val resultingRoute: PathMatcher[Tuple1[String]] = routeDef.reduceLeft(join)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读