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.
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
The content of the file is:
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
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
.
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
Why is concat
not found?
Note that if I use the Scala console from within the sbt-shell
and execute the same commands then everything works.
How can I configure IntelliJ to work with a Worksheet without issues?
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
Possibly related to issue SCL-12890
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With