Scala, cats, http4s - 无法识别 Http4s 中的 <+> 符号



我创建了两条Http4s路由:

class FirstRoutes[F[_] : Async](service: FirstService[F]) extends Http4sDsl[F] {        
  def routes: HttpRoutes[F] = HttpRoutes.of[F] {
        //... some code
  }
}   
class SecondRoutes[F[_] : Async] extends Http4sDsl[F] {            
    def routes: HttpRoutes[F] = HttpRoutes.of[F] {
    //... some code
    }
}

现在在我的main方法中,我想像这样调用此路由:

override def run(args: List[String]): IO[ExitCode] =
    for {
      _ <- {
        val app = {
          //...
          val firstRoutes = new FirstRoutes[F](someService)
          val secondRoutes = new SecondRoutes[F]
          (firstRoutes.routes <+> secondRoutes.routes).orNotFound
        }

但是当我编译这段代码时,我得到了一个错误:

Error:(26, 33) value <+> is not a member of org.http4s.HttpRoutes[Server.F]
          (firstRoutes.routes <+> secondRoutes.routes).orNotFound

这对我来说很奇怪,因为当我在路由类上使用ctrl+空格时,我通常可以使用这个<+>符号,而且我也有很好的导入:

import cats.effect._
import cats.data._
import org.http4s.server.blaze.BlazeServerBuilder
import cats.effect._
import cats.implicits._

无法了解如何解决此问题并使用<+>调用路由类。也许是智能问题?有人可以帮助我吗?

为 build.sbt 添加此 scala 选项

scalacOptions += "-Ypartial-unification"

也许你应该读 http://eed3si9n.com/herding-cats/import-guide.html

https://blog.softwaremill.com/9-tips-about-using-cats-in-scala-you-might-want-to-know-e1bafd365f88 建议 2(

<+>来自cats.syntax.semigroupk._.

以下代码编译

import cats.effect.{Async, ExitCode, IO}
import cats.syntax.semigroupk._
import org.http4s.HttpRoutes
import org.http4s.dsl.Http4sDsl
import org.http4s.syntax.kleisli._
import scala.language.higherKinds
class App {
  class FirstRoutes[F[_] : Async](service: FirstService[F]) extends Http4sDsl[F] {
    def routes: HttpRoutes[F] = HttpRoutes.of[F] {
      ???
    }
  }
  class SecondRoutes[F[_] : Async] extends Http4sDsl[F] {
    def routes: HttpRoutes[F] = HttpRoutes.of[F] {
      ???
    }
  }
  trait FirstService[F[_]]
  /*override*/ def run[F[_]: Async](args: List[String]): IO[ExitCode] = {
    val someService: FirstService[F] = ???
    for {
      _ <- {
        val app = {
          //...
          val firstRoutes = new FirstRoutes[F](someService)
          val secondRoutes = new SecondRoutes[F]
          (firstRoutes.routes <+> secondRoutes.routes).orNotFound
        }
        IO(???)
      }
    } yield ExitCode(1)
  }
}

相关内容

  • 没有找到相关文章

最新更新