gpt4 book ai didi

scala - SBT 错误的编译顺序

转载 作者:行者123 更新时间:2023-12-04 13:17:41 25 4
gpt4 key购买 nike

TL/DR:检查最后两次提交并运行 sbt clean compile :https://github.com/mirelon/akka-in-action/tree/json/chapter2

有 RestInterface.scala:

package com.goticks

import akka.actor._

import spray.routing._
import spray.http.StatusCodes
import spray.httpx.SprayJsonSupport._
import spray.routing.RequestContext
import akka.util.Timeout
import scala.concurrent.duration._
import scala.language.postfixOps

class RestInterface extends HttpServiceActor
with RestApi {
def receive = runRoute(routes)
}

trait RestApi extends HttpService { actor: Actor =>
import context.dispatcher
import com.goticks.TicketProtocol._

implicit val timeout = Timeout(10 seconds)
import akka.pattern.ask

def routes: Route =

path("events") {
get { requestContext =>
context.actorOf(Props[Resu]).ask(GetEvents)
}
}

}

class Responder(requestContext:RequestContext) extends Actor {
import TicketProtocol._

def receive = {

case Events(events) =>
requestContext.complete(StatusCodes.OK, events)
self ! PoisonPill

}
}

和 Resu.scala(注意 Resu 在 RestInterface 之后按字母顺序排列)
package com.goticks

import akka.actor.Actor

class Resu extends Actor {
import TicketProtocol._
import spray.json._

def receive = {

case GetEvents => {
println(Event(event = "E").toJson)
}
}
}

object TicketProtocol {
import spray.json._

case class Event(event:String)

case object GetEvents

case class Events(events:List[Event])

object Event extends DefaultJsonProtocol {
implicit val format = jsonFormat1(Event.apply)
}

}

构建.sbt:
name := "goticks"

version := "0.1-SNAPSHOT"

organization := "com.goticks"

scalaVersion := "2.11.1"

libraryDependencies ++= {
val akkaVersion = "2.3.4"
val sprayVersion = "1.3.1"
Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"io.spray" %% "spray-can" % sprayVersion,
"io.spray" %% "spray-routing" % sprayVersion,
"io.spray" %% "spray-json" % "1.2.6"
)
}

build.properties;
sbt.version=0.13.7

插件.sbt:
resolvers += Classpaths.typesafeResolver

resolvers += "sbt-idea" at "http://mpeltonen.github.com/maven/"

addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2")

addSbtPlugin("com.typesafe.sbt" % "sbt-start-script" % "0.10.0")

addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.3.8")

编译问题

当我跑 sbt clean compile , 一切都好。但是当我重构 Resu类成 Ress (注意 Ress 在字母顺序之前 RestInterface )(也重命名它的文件,差异可以在这里查看: https://github.com/mirelon/akka-in-action/commit/583ca801fb7d1564024eee2f98d57f03ecacc6e5 ),然后有一个编译错误:
[error] /home/miso/IdeaProjects/akka-in-action/chapter2/src/main/scala/com/goticks/Ress.scala:12: Cannot find JsonWriter or JsonFormat type class for com.goticks.TicketProtocol.Event
[error] println(Event(event = "E").toJson)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed

显然,缺少一个隐式的 json 编写器。可能是因为编译顺序不正确? sbt 可以按字母顺序编译类,而忽略导入吗?

最佳答案

是的,sbt 总是按字母顺序编译。问题是 sbt 在运行编译器之前不知道文件之间的依赖关系。

您所看到的是 scala 编译器本身取决于源文件的顺序。 Sbt 总是对源文件进行排序,因此您至少可以通过按有效的顺序放置代码来解决这些问题。

我不是 100% 确定为什么你会从一些隐式 + 宏黑客中遇到这样的问题,但这可能是错误的。

关于scala - SBT 错误的编译顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28053471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com