首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在从Scala 2.13迁移到3.2时丢失

在从Scala 2.13迁移到3.2时丢失
EN

Stack Overflow用户
提问于 2022-09-27 04:23:43
回答 2查看 126关注 0票数 1

我一直按照迁移指南上的指示行事,但没有成功。我已经有了一个跨编译的SBT项目,没有问题,有以下build.sbt

代码语言:javascript
复制
lazy val ttv = (project in file("."))
  .settings(
    name := "Tame the Void",
    version := "0.1",
    scalaVersion := "3.2.0",
    crossScalaVersions := Seq("2.13.6", "3.2.0"),
    libraryDependencies ++= mainDependencies ++ testDependencies,
    scalacOptions := {
      Seq(
        "-encoding",
        "UTF-8",
        "-feature",
        "-language:implicitConversions",
        // disabled during the migration
        // "-Xfatal-warnings"
      ) ++
        (CrossVersion.partialVersion(scalaVersion.value) match {
          case Some((3, _)) =>
            println("Using 3.0 flags --------------------")
            Seq(
              "-unchecked",
              "-source:3.2-migration",
              "-indent",
              "-rewrite"
            )
          case _ =>
            println("Using 2.13 flags --------------------")
            Seq(
              "-deprecation",
              "-Wunused:imports,privates,locals",
              "-Wvalue-discard"
            )
        })
    }
  )

所有Scala2.13代码都编译得很好,但是我添加了一个scala 3特定的文件来测试:src/main/scala-3/Scala3test.scala

代码语言:javascript
复制
object Scala3Test extends App {

  given x: Int = 1
  def f(using x: Int) = println(x)
  f

  given Ordering[Int] with
    override def compare(x: Int, y: Int): Int = if x < y then 1 else -1

  def g(using x: Ordering[Int]) = println(x.compare(3, 4))
  g
}

如果我从头创建Scala 3项目,这个测试运行得很好,但是这里似乎根本不识别新的语法:

代码语言:javascript
复制
compile
Using 3.0 flags --------------------
[info] compiling 102 Scala sources to D:\Projects\4x-scala\target\scala-3.2.0\classes ...
[warn] Flag -source set repeatedly
[error] -- [E018] Syntax Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:9 
[error] 3 |  given x: Int = 1
[error]   |         ^
[error]   |         expression expected but : found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:15 ----------
[error] 3 |  given x: Int = 1
[error]   |               ^
[error]   |               end of statement expected but '=' found
[error] -- [E018] Syntax Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:7:16 
[error] 7 |  given Ordering[Int] with
[error]   |                ^
[error]   |                expression expected but '[' found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E006] Not Found Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:3:2 
[error] 3 |  given x: Int = 1
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:5:3 -----------
[error] 5 |  f
[error]   |   ^
[error]   |No given instance of type Int was found for parameter x of method f in object Scala3Test
[error] -- [E006] Not Found Error: D:\Projects\4x-scala\src\main\scala-3\Scala3Test.scala:7:2 
[error] 7 |  given Ordering[Int] with
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[warn] one warning found
[error] 6 errors found

我的理解是,基于SBT的交叉构建文档应该在3.2.0中构建该文件并识别语法。它也没有重写任何语法(应该给出"-indent“、"-rewrite”标志)。

任何指示都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-28 13:38:44

你在某个地方设置了错误的旗子。请允许我表明:

代码语言:javascript
复制
$ ls
Scala3Test.scala build.sbt
$ cat build.sbt
name := "scala-3-test"
scalaVersion := "3.2.0"
scalacOptions += "-source:3.0-migration"
$ sbt compile
[info] compiling 1 Scala source to ***/target/scala-3.2.0/classes ...
[error] -- [E018] Syntax Error: ***/Scala3Test.scala:3:9
[error] 3 |  given x: Int = 1
[error]   |         ^
[error]   |         expression expected but : found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: ***/Scala3Test.scala:3:15 -----------
[error] 3 |  given x: Int = 1
[error]   |               ^
[error]   |               end of statement expected but '=' found
[error] -- [E018] Syntax Error: ***/Scala3Test.scala:7:16
[error] 7 |  given Ordering[Int] with
[error]   |                ^
[error]   |                expression expected but '[' found
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- [E006] Not Found Error: ***/Scala3Test.scala:3:2
[error] 3 |  given x: Int = 1
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] -- Error: ***/Scala3Test.scala:5:3 ------------
[error] 5 |  f
[error]   |   ^
[error]   |No given instance of type Int was found for parameter x of method f in object Scala3Test
[error] -- [E006] Not Found Error: ***/Scala3Test.scala:7:2
[error] 7 |  given Ordering[Int] with
[error]   |  ^^^^^
[error]   |  Not found: given
[error]   |
[error]   | longer explanation available when compiling with `-explain`
[error] 6 errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 3 s, completed 28 Sep 2022, 15:23:17

现在我禁用了-source:3.0-migration标志。

代码语言:javascript
复制
$ cat build.sbt
name := "scala-3-test"
scalaVersion := "3.2.0"
//scalacOptions += "-source:3.0-migration"
$ sbt compile
[info] compiling 1 Scala source to ***/target/scala-3.2.0/classes ...
[success] Total time: 3 s, completed 28 Sep 2022, 15:23:56

此外,编译器警告您注意Flag -source set repeatedly,即使您在构建文件中只设置了一次,这也意味着您也在其他地方设置了它。

如果您的项目中真的没有其他构建代码,包括可以设置一些您不知道的标志的插件,那么也许您的$HOME/.sbt/1.0文件夹中有一些允许插件或设置标志的代码。

票数 1
EN

Stack Overflow用户

发布于 2022-09-27 09:59:59

移除

代码语言:javascript
复制
"-source:3.2-migration",

会有帮助的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73862373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档