在IntelliJ中使用Scala工作表很难 [英] Hard time using Scala Worksheets in IntelliJ

查看:80
本文介绍了在IntelliJ中使用Scala工作表很难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循Scala中的函数编程原理课程 但是在IntelliJ中使用Scala工作表进行快速测试时遇到很多问题.

I'm following the course Functional Programming Principles in Scala but I am experiencing a lot of issues when using Scala Worksheets in IntelliJ to make quick tests.

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

For example, I have set up a new Scala project where I have created a package object called lecture5 (it's in the file) 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)
    }
}

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

In the worksheet I have the following:

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 = REPL(由于某些原因Plain不起作用)和Compiler profile = Default.

In the settings for the worksheet I checked Interactive Mode, Make project before run and use Run Type = REPL (Plain doesn't work for some reason) and Compiler profile = Default.

当我单击"play"按钮运行工作表时,功能initlast起作用,但是对于功能concat,我得到了错误:

When I click on the "play" button to run the worksheet the functions init and last work, but for the function concat I get error:

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

为什么找不到concat?

请注意,如果我从sbt-shell内部使用Scala控制台并执行相同的命令,则一切正常.

Note that if I use the Scala console from within the sbt-shell and execute the same commands then everything works.

如何配置IntelliJ以使其与工作表一起使用而不会出现问题?

How can I configure IntelliJ to work with a Worksheet without issues?

推荐答案

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

I replicated the issue on IntelliJ 2019.1.2, Scala Plugin 2019.1.8. No form of building the project before running the worksheet worked. Package object was finally successfully imported after Invalidate Caches / Restart.... A workaround that seems to work for me without restarting is to use Scala Scratch file instead of Scala Worksheet:

Right click project | New | Scratch file | Scala

可能与问题 SCL-12890

这篇关于在IntelliJ中使用Scala工作表很难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆