鉴于以下内容:
的src/main /斯卡拉/网/ Equals5.scala
package net
import scala.language.experimental.macros
import scala.reflect.macros.Context
case class Equals5(value: Int) {
require(value == 5)
}
object Equals5 {
implicit def wrapInt(n: Int): Equals5 = macro verifyIntEquals5
def verifyIntEquals5(c: Context)(n: c.Expr[Int]): c.Expr[Equals5] = {
import c.universe._
val tree = n.tree match {
case Literal(Constant(x: Int)) if x == 5 =>
q"_root_.net.Equals5($n)"
case Literal(Constant(x: Int)) =>
c.abort(c.enclosingPosition, s"$x != 0")
case _ =>
q"_root_.net.Equals5($n)"
}
c.Expr(tree)
}
}
Run Code Online (Sandbox Code Playgroud)
build.sbt
val paradiseVersion = "2.1.0-M5"
scalaVersion := "2.11.7"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.11.7"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "3.0.0-M7"
Run Code Online (Sandbox Code Playgroud)
项目/ build.properties
sbt.version=0.13.0
Run Code Online (Sandbox Code Playgroud)
我可以成功编译,但尝试运行以下测试:
的src /测试/阶/净/ Equals5Test.scala
package net
import org.scalatest.Matchers
import org.scalatest._
import org.scalatest.prop.Checkers._
class Equals5Test extends FlatSpec with Matchers {
"Trying to create an `Equals5` case class with an invalid Int" should "fail to compile" in {
"Equals5(-555)" shouldNot compile
}
}
Run Code Online (Sandbox Code Playgroud)
给出编译时错误:
.../Equals5Test.scala:11: can't expand macros compiled
by previous versions of Scala
[error] "Equals5(-555)" shouldNot compile
[error] ^
Run Code Online (Sandbox Code Playgroud)
看看这个答案,我预计使用scala 2.11与sbt 0.13.0将解决这个问题.
请让我知道如何解决此编译时错误.
Ale*_*nov 12
您特别请求为Scala 2.10编译的ScalaTest版本,因此它的宏compile将无法正确扩展(并且很可能在其他方面也不兼容Scala 2.11).(目前的SBT版本也是0.13.9,所以你可能也希望更新它.)
| 归档时间: |
|
| 查看次数: |
8226 次 |
| 最近记录: |