Mike Slinn

Scala Worksheets

— Draft —

Published 2013-12-01. Last modified 2024-07-20.
Time to read: 2 minutes.

Scala worksheets are an imaginative melding of the Scala REPL and an IDE. Visual Studio Code and IntelliJ worksheets are explored in this lecture.

Scala worksheets are an imaginative melding of the Scala REPL and an IDE. Both IntelliJ and Visual Studio Code/Metals worksheets are explored in this lecture. We discussed Scala for Visual Studio Code in an earlier lecture. Visual Studio Code/Metals worksheets rely on Ammonite, which we discussed earlier.

Filetypes

Worksheets created by IntelliJ have a .sc file type, while Visual Studio Code/Metals requires a .worksheet.sc file type. If you want a worksheet to be useable from either IDE, give it a .worksheet.sc file type.

Worksheets consist of the Scala code they contain. If a worksheet resides within an SBT project, the project code will be accessible from the worksheet because the project code will be on the classpath..

Classpath

Worksheets have all of the limitations of the REPL, however there is no :paste command to provide a workaround. This means you cannot define a companion object with a companion class in a worksheet. However, you can reference a companion class/object defined in your program because the worksheet inherits the project classpath.

The courseNotes project contains a file called ClassWithCompanion.scala that defines a class called ClassWithCompanion, and a companion class. Companion objects and companion classes will be discussed in the Class Part 1 lecture in this course. Those definitions are referenced in the worksheets.

It is good to save your worksheets as a reference so you can remember how you derived an obscure incantation. If you use worksheets you won’t need to use the REPL much, if ever.

IDEA Worksheets

IDEA worksheets do not contain the last evaluation values as comments. Let’s play with the IDEA worksheet in the courseNotes directory.

  1. Click on Open Project.
  2. Navigate to the courseNotes directory and double-click on build.sbt.

At the time this lecture was written there was an IntelliJ IDEA bug that prevented this from working. Instead, we can open the project as follows.

  1. From a bash prompt, navigate to the courseNotes directory.
  2. Type:
    Scala REPL
    scala> sbt gen-idea no-classifiers
  3. From IntelliJ IDEA, click on Open Project.
    1. Navigate to the courseNotes directory and select the directory (do not click on build.sbt)
  4. Click on Choose.

To open and run the IDEA worksheet that I have prepared:

  1. Reveal the Project panel tab if necessary by clicking on the small unmarked square at the bottom left of the IDEA window and selecting Project.
  2. Open the Project panel by clicking on the Project tab.
  3. Open the courseNotes project by double-clicking on it in the Project panel.
  4. Open the file by double-clicking on src / main / scala / IdeaWorksheet.sc.
  5. Run the worksheet by clicking on the green play button at the top of the worksheet. The worksheet pane will divide in half, with the Scala code on the left and the evaluation results on the right. Unfortunately, IDEA worksheets are not evaluated upon save. The key binding for evaluation is Ctrl / Option / W for all OSes.
  6. Create a new line, and enter an expression like 21 * 2, then run the worksheet. The result is shown to the right of the expression. Unlike the REPL and unlike Scala-IDE, no name for the value is shown.

* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.