A worksheet is a Scala file that is evaluated on save, and the result of each expression is shown
in a column to the right of your program. Worksheets are like a REPL session on steroids, and
enjoy 1st class editor support: completion, hyperlinking, interactive errors-as-you-type, etc.
Worksheets use the extension .worksheet.sc
.
In the following, we show how to use worksheets in IntelliJ, and in VS Code (with the Metals extension).
- Open a Scala project, or create one.
- To create a project in IntelliJ, select “File” -> “New” -> “Project…”, select “Scala” in the left column, and click “Next” to set the project name and location.
- To create a project in VS Code, run the command “Metals: New Scala project”, select the
seed
scala/scala3.g8
, set the project location, open it in a new VS Code window, and import its build.
- Create a file named
hello.worksheet.sc
in the directorysrc/main/scala/
.- In IntelliJ, right-click on the directory
src/main/scala/
, and select “New”, and then “File”. - In VS Code, right-click on the directory
src/main/scala/
, and select “New File”.
- In IntelliJ, right-click on the directory
- Paste the following content in the editor:
println("Hello, world!") val x = 1 x + x
- Evaluate the worksheet.
- In IntelliJ, click on the green arrow at the top of the editor to evaluate the worksheet.
- In VS Code, save the file.
You should see the result of the evaluation of every line on the right panel (IntelliJ), or as comments (VS Code).
A worksheet evaluated in IntelliJ.
A worksheet evaluated in VS Code (with the Metals extension).
Note that the worksheet will use the Scala version defined by your project (set by the key scalaVersion
,
in your file build.sbt
, typically).
Also note that worksheets don’t have a program entry point. Instead, top-level statements and expressions are evaluated from top to bottom.
Contributors to this page:
Contents
- Introduction
- Scala Features
- Why Scala 3?
- A Taste of Scala
- Hello, World!
- The REPL
- Variables and Data Types
- Control Structures
- Domain Modeling
- Methods
- First-Class Functions
- Singleton Objects
- Collections
- Contextual Abstractions
- Toplevel Definitions
- Summary
- A First Look at Types
- String Interpolation
- Control Structures
- Domain Modeling
- Tools
- OOP Modeling
- FP Modeling
- Methods
- Method Features
- Main Methods in Scala 3
- Summary
- Functions
- Anonymous Functions
- Function Variables
- Eta-Expansion
- Higher-Order Functions
- Write Your Own map Method
- Creating a Method That Returns a Function
- Summary
- Packaging and Imports
- Scala Collections
- Collections Types
- Collections Methods
- Summary
- Functional Programming
- What is Functional Programming?
- Immutable Values
- Pure Functions
- Functions Are Values
- Functional Error Handling
- Summary
- Types and the Type System
- Inferred Types
- Generics
- Intersection Types
- Union Types
- Algebraic Data Types
- Variance
- Opaque Types
- Structural Types
- Dependent Function Types
- Other Types
- Contextual Abstractions
- Extension Methods
- Context Parameters
- Context Bounds
- Given Imports
- Type Classes
- Multiversal Equality
- Implicit Conversions
- Summary
- Concurrency
- Scala Tools
- Building and Testing Scala Projects with sbt
- Worksheets
- Interacting with Java
- Scala for Java Developers
- Scala for JavaScript Developers
- Scala for Python Developers
- Where To Go Next