Calling a shortcut in the akka http route list gives a compilation error (implicit value for connecting parameters)

Given the following routes

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

I can easily identify

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

getting the expected type (PathMatcher [Tuple [String]]).

But creating a route programmatically, as in

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

won't compile giving me

could not find an implicit value for connecting parameters: akka.http.scaladsl.server.util.TupleOps.Join [_1, _1]

Also, the derived type resultRoute is

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

I would really appreciate any hint giving me some idea of ​​what I'm doing wrong here or how this can be resolved.

For completeness, here is my import:

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

Many thanks!

+4
source share
2 answers

, routeDef , List[PathMatcher[_ >: Tuple1[String] with Unit]].

, (a: PathMatcher[L])./(b: PathMatcher[R]) a TupleOps.Join[L, R]: akka.http.scaladsl.server.PathMatcher. PathMatcher routeDef.


shapeless, ( HList ):

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)
+1

.

1.

reduce, reduceLeft reduceRight, ,

(a / b) / c === a / (b / c)

.

, .

2.

- undefined , . - reduceOption.

0

Source: https://habr.com/ru/post/1687630/


All Articles