Scala 3 worksheet issues in IntelliJ

I just tried using worksheets in IntelliJ, but immediately encountered several issues:

  1. Definitions are compiled in-order (precluding mutual recursion and proper code organization).
  2. Method overloading is not supported.
  3. Building the project also compiles the worksheet as if it were a normal Scala file, leading to highlighting errors about illegal toplevel definitions.

Does anyone know how such issues can be avoided?

P.S.: I was using Scastie before, but kept running into limitations due to it being browser-based. I have also tried to switch to Visual Studio Code several times over the years, but I simply cannot remember how to do even the most basic things (like creating a new project) in that IDE, as it feels terribly unintuitive to me.

IntelliJ’s Scala 3 support is not very good right now (they are working on it).

Try VS Code with Metals extension. Create your project with SBT on the command line / Terminal: sbt new scala/scala3.g8, then open the folder with build.sbt in it with the “Open Folder” option in VS Code.

You can also use the Command Palette (Ctrl+Shift+P) then type “Metals” and there is an option to create a new worksheet:

Another option is to use Scala-cli which is an awesome new tool. Install it, then use scala-cli setup-ide . in a folder, and then you can open that folder in VS Code and Metals will see it. Here I have an example Scala-cli project where I typed up all the Toolkit tutorials including worksheets and test files.

2 Likes

Regarding scala-cli, how can I use it to build and run an sbt project?

Scala-cli is an alternative to sbt for small, single-module projects. It’s not a “full build tool.” This is intended by design. No support for multi-modules or custom tasks like sbt.

If you have a simple, single-module sbt project though, it can also be done with scala-cli. In an empty directory, run scala-cli setup-ide . for use with, say VS Code and Metals. Then you can add your dependencies to a file named project.scala in the same directory with the using directives. Here’s an example I’m using for some quick scripts:

// this is file project.scala
//> using scala 3.3.1
//> using jvm 17
//> using toolkit latest
//> using dep org.apache.commons:commons-numbers-combinatorics:1.1
//> using dep org.scala-lang.modules::scala-parallel-collections:1.0.4
//> using dep co.computist::augment:0.0.3

You can look up the dependencies syntax on Scaladex, for example:

Then either use Metals (which will have a run button over @main methods) or on the Terminal scala-cli run . Same with scala-cli test . to run all the tests. This shows using worksheets, and Metals Testing tab, and the Terminal, whichever you prefer:

It works with IntelliJ too but I haven’t used it. If you want to use it alongside an sbt project, there is a way to do that too.

There is way more stuff in the docs, also check the Cookbook. It’s a really awesome tool. In 3.4 release scala-cli will replace the scala command.

2 Likes