【问题标题】:Hard time using Scala Worksheets in IntelliJ在 IntelliJ 中使用 Scala 工作表很难
【发布时间】:2019-05-19 08:31:07
【问题描述】:

我正在学习 Scala 中的函数式编程原理课程 但是我在 IntelliJ 中使用 Scala Worksheets 进行快速测试时遇到了很多问题。

例如,我建立了一个新的 Scala 项目,并在其中创建了一个名为 lecture5 的包对象(在文件中)src/main/scala/lecture5/package.scala

文件内容为:

package object lecture5 {

  def last[T](xs:List[T]): T =  xs match {
    case List() => throw new Error("empty list")
    case List(x) => x
    case x :: y => last(y)
  }

  /* init should return all elements but last */
  def init[T](xs: List[T]): List[T] = xs match {
    case List() => throw new Error("List is empty")
    case List(x) => List[T]()
    case y :: ys => y :: init(ys)
  }

  def concat[T](xs: List[T], ys: List[T]): List[T] = xs match {
      case List() => ys
      case z:: zs => z :: concat(zs, ys)
    }
}

在工作表中我有以下内容:

import lecture5._


val x = List("a","b","c")

val xs = List("a","b")
val ys = List("c")

last(x)

init(x)

concat(xs, ys) == x

在工作表的设置中,我检查了Interactive ModeMake project before run 并使用了Run Type = REPLPlain 由于某种原因不起作用)和Compiler profile = Default

当我单击“play”按钮运行工作表时,函数 initlast 工作,但函数 concat 出现错误:

Error:(13, 9) not found: value concat
concat(xs, ys) == x

为什么找不到concat

请注意,如果我在 sbt-shell 中使用 Scala 控制台并执行相同的命令,那么一切正常。

如何将 IntelliJ 配置为使用工作表而不会出现问题?

【问题讨论】:

    标签: scala intellij-idea


    【解决方案1】:

    我在 IntelliJ 2019.1.2、Scala 插件 2019.1.8 上复制了该问题。在运行工作表之前构建项目的任何形式都不起作用。在Invalidate Caches / Restart...之后终于成功导入包对象。一个似乎对我有用而无需重新启动的解决方法是使用 Scala Scratch file 而不是 Scala Worksheet

    Right click project | New | Scratch file | Scala
    

    可能与问题SCL-12890有关

    【讨论】:

      猜你喜欢
      • 2011-02-09
      • 2017-05-31
      • 2014-06-07
      • 1970-01-01
      • 2023-02-20
      • 1970-01-01
      • 2011-07-28
      • 2013-04-28
      • 1970-01-01
      相关资源
      最近更新 更多