luc*_*one 5 scala intellij-idea
我正在遵循Scala中的函数编程原理课程,但是在IntelliJ中使用Scala工作表进行快速测试时遇到了很多问题。
例如,我建立了一个新的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)
}
}
Run Code Online (Sandbox Code Playgroud)
在工作表中,我具有以下内容:
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
Run Code Online (Sandbox Code Playgroud)
在工作表的设置中,我检查了Interactive Mode,Make project before run然后使用Run Type = REPL(Plain由于某种原因不起作用)和Compiler profile = Default。
当我点击“ play”按钮,运行工作职能init和last工作,但功能concat我得到的错误:
Error:(13, 9) not found: value concat
concat(xs, ys) == x
Run Code Online (Sandbox Code Playgroud)
为什么concat找不到?
请注意,如果我从中使用Scala控制台sbt-shell并执行相同的命令,则一切正常。
如何配置IntelliJ以使其与工作表一起使用而不会出现问题?
我在 IntelliJ 2019.1.2、Scala Plugin 2019.1.8 上复制了该问题。在运行工作表之前构建项目的任何形式都不起作用。包对象终于导入成功了Invalidate Caches / Restart...。一个似乎对我有用而无需重新启动的解决方法是使用 ScalaScratch file而不是Scala Worksheet:
Right click project | New | Scratch file | Scala
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |