SBT 0.13.0 - 无法扩展以前版本的Scala编译的宏

时间:2015-08-25 03:48:37

标签: scala

鉴于以下内容:

的src /主/阶/净/ 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)
  }
}

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"

项目/ build.properties

sbt.version=0.13.0

我可以成功编译,但尝试运行以下测试:

的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
    }
}

给出了编译时错误:

.../Equals5Test.scala:11: can't expand macros compiled 
      by previous versions of Scala
[error]         "Equals5(-555)" shouldNot compile
[error]                                   ^

查看此answer,我预计使用带有sbt 2.11的scala 0.13.0可以解决此问题。

请告诉我如何解决此编译时错误。

1 个答案:

答案 0 :(得分:12)

您特别请求为Scala 2.10编译的ScalaTest版本,因此其$sql_update1 = "UPDATE `registration` SET `Current_Bill`= $updatebill WHERE `Reg_id` = $regid"; $sql_update2 = "UPDATE `billing_month` SET `Current_Dues`= $updatecurrentbill WHERE `Bill_id` = $billid"; $sql_update3 = "UPDATE `registration` SET `Current_Surcharge`= $updatesurcharge WHERE `Reg_id` = $regid"; $sql_update4 = "UPDATE `billing_month` SET `Surcharge`= $updatedsurcharge WHERE `Bill_id` = $billid"; mysqli_query($link, $sql_update1); mysqli_query($link, $sql_update2); mysqli_query($link, $sql_update3); mysqli_query($link, $sql_update4); 之类的宏不能正确扩展(并且它很可能与Scala 2.11无法兼容)其他方式)。 (目前的SBT版本也是0.13.9,所以你可能也希望更新它。)