【问题标题】:IntelliJ IDEA underlines my Scala code with implicits, but code worksIntelliJ IDEA 用隐式强调了我的 Scala 代码,但代码有效
【发布时间】:2017-02-23 12:48:47
【问题描述】:

这是完整的代码:

   package com.example.controller

import spray.routing.{HttpService, Route}
import spray.http.StatusCodes

import scala.util.{Failure, Success}
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

trait RentController extends HttpService{

  var books: List[String] = List("1","2","3","4")

  def rentBook(intNum: Int): Future[Unit] = Future[Unit] {
    val book = books.find(p => p.equals(intNum.toString))
    if(book.head.equals("4")) throw new UnsupportedOperationException()
  }

  def giveBackBook(intNum: Int): String = {
    val book = books.find(p => p.equals(intNum.toString))
    book.head
  }

  val rent: Route =
    pathPrefix("rent") {
      path("book" / IntNumber) { intNum =>
        get {
          onComplete(rentBook(intNum)) {
            case Success(_) => complete(StatusCodes.OK)
            case Failure(_) => complete(StatusCodes.Accepted)
          }
        }
      }
    }

  val giveBack: Route =
    pathPrefix("giveBack") {
      path("book" / IntNumber) { intNum =>
        get {
          onSuccess(Future.successful(giveBackBook(intNum))) {
            extraction: String => complete(extraction)
          }
        }
      }
    }

}

奇怪的是,他只强调了 OnSuccess,但 OnComplete 显然对他来说是正确的。代码有效,但我想知道我是否可能在某个地方犯了一些错误。所以请告诉我是否缺少一些导入,或者这只是 IntelliJ 错误。

编辑

我更改了代码,现在函数rendBook和giveBackBook逻辑上不正确,并且没有做太多,但你应该很容易重现它

【问题讨论】:

  • 你能发一个minimal reproducible example吗?复制粘贴对任何人都不起作用。
  • Bug report is welcome 附有一个要复制的示例项目。
  • @YuvalItzchakov 如果您仔细观察,这是最小、完整和可验证的示例:) 两条路线,一条有效,一条无效,所有进口 :) 我认为需要这段代码的每一段来验证我的问题:)
  • @CrazyCoder 我知道,但我想从一些 Scala 专家那里获得专业知识,如果我没有犯任何错误的话 :)
  • @KamilBanaszczyk import com.example.repository.BookRepository 将如何为我编译?

标签: scala intellij-idea akka spray


【解决方案1】:

这是 IntelliJ 的 Scala 插件中的 a bug - 过度使用隐式可能会让你到达那里。如果你知道磁铁模式——这也是Spray uses,它需要大量的隐式转换来克服 Scala 中的一些重载限制。

我在使用磁铁模式实现某些功能时遇到了这个问题,因此不仅使用 Spray - 一些代码带下划线,而其余代码则没有,但它仍然有效。

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多