contributor for Scala Center
Out brand new initiative to focus on technical aspects of the work done by SoftwareMill and VirtusLab
Come to our booth!
1. How to install Scala?
2. How to write Scala?
3. How to compile Scala?
4. How to run Scala or test it?
5. How and which libraries do I get?
6. How to fix errors?
7. How to refactor Scala code?
After each question will come a Q&A section.
Go to slido.com and enter 3001 code
Please post any issues that come to mind when thinking about a particular topic. I will try to answer at least one question after each section.
Mill
But why do we need another package manager?
The new oficial scala runner
Soon in all the best package managers!
> brew install virtuslab/scala-experimental/scala
OR
> cs setup
> cs install scala-experimental
./hello.scala
@main
def hello = println(“Hello Scala CLI!”)
> scala hello.scala
Hello Scala CLI!
package managers not yet updated, this might take a while
on scala-lang we'll get two ways of installing, via coursier and or via scala package
Scala CLI is directed towards small projects or scripts, so users still need to figure out a build tool
Protocol that defines the exchanges between client and the server to provide IDE features for a programming language
uses JSON RPC
backed by Microsoft used by open source
abstracts language support from UI
well defined and supported in multiple editors
Makes it easy to focus on making sure that the language is supported and doesn’t require any knowledge of the editor.
- you should not need to learn a new editor for a new language
- "just use Intellij" is not an answer (unless you are used to that)
- tooling is important for the language, but language should not be just the IDE
Depends!
Metals or Intellij?
Just try it out!
You can ask around your team or the community for pointers.
Stick with whatever editor you are used to.
You should have a choice to do that.
Your machine is older?
You want a ready workspace?
You don't want to set up the environment?
Metals via Gitpod or Github Codespaces
scalameta.org/metals
we need more guides on setting up
making single file work for users, maybe with default Scala CLI
Metals stability issues
Intellij support issues for Scala 3 (highly encourage people to report any issues and use the nightly)
no exploration of environment setup for and blessed way of doing things
- obviously done by the compiler
- but how do we actually interact with the compiler while writing code?
- no one really wants to invoke the compiler manually each time
BSP Server
LSP Server
LSP Server
(e.g. Metals)
Editor
(e.g. VS Code)
BSP Server
(e.g. Bloop)
completions
hover
refrences
...
compile
test
run
...
sbt
mill
Bazel
Maven
Gradle
Bleep
...
Bloop - build server
sbt - works as single server, for a single project
mill - same as sbt
Bazel BSP
Scala CLI
Bleep
What provides the best getting started experience?
maturity of some BSP servers still needs more work
sbt BSP limited to one task being executed
stability issues
Bloop most mature and useful general purpose dire need of refactoring
Bloop has a fork :(
Still ongoing work for Bazel BSP
> cat main.scala
@main def hello() = println("Hello world!")
> scala run main.scala
Hello world!
> cat main2.sc
println("Hello world!")
> scala run main.sc
Hello world!
> scala run --js main.sc
Downloading Scala.js CLI
Downloading 2 dependencies
Compiling project (Scala 3.3.0, Scala.js)
Compiled project (Scala 3.3.0, Scala.js)
Hello world!
> scala run --native .
Compiling project (Scala 3.3.0, Scala Native)
Compiled project (Scala 3.3.0, Scala Native)
[info] Linking (1680 ms)
[info] Discovered 683 classes and 3779 methods
[info] Optimizing (debug mode) (1523 ms)
[info] Generating intermediate code (1300 ms)
[info] Produced 8 files
[info] Compiling to native code (2692 ms)
[info] Total (7465 ms)
Hello world!
> scala-cli . --debug --debug-mode attach --debug-port 5004
Listening for transport dt_socket at address: 5004
//> using test.dep org.scalameta::munit::1.0.0-M7
class MyTests extends munit.FunSuite {
test("foo") {
assert(2 + 2 == 4)
}
}
> scala test .
Compiling project (Scala 3.3.0, JVM)
Compiled project (Scala 3.3.0, JVM)
Compiling project (test, Scala 3.3.0, JVM)
Compiled project (test, Scala 3.3.0, JVM)
MyTests:
+ foo 0.01s
Scala CLI allows for specifying anything you would on the commandline in special comments.
//> using scala 3.3.0
//> using platform native
//> using options -Wunused -Wvalue-discard
I highly recommend taking a look at the documentation of Scala CLI as it mentions all the possibilities
And Scala CLI!
Couple of different ways of running.
Uses scala debug adapter library and DAP protocol to provide debugging support in all editors that implement it.
Tests in Metals could be quicker.
Regression in script support for Scala CLI 1.0.0 (soon fixed)
debugging always a complex topic, but more improvements are coming
coursier resolve _
Metals reuse that mechanism for Ammonite, Worksheet, Scala CLI and sbt completions.
Intellij will have suggestions, but this is a bit limited
There is an additional plugin for VS Code that uses the Scaladex API
manually via Scaladex
Is the question!
//> using toolkit latest
class MyTests extends munit.FunSuite {
test("foo") {
assert(2 + 2 == 4)
}
}
//> using toolkit typelevel:latest
import cats.effect.*
import fs2.io.file.Files
object Hello extends IOApp.Simple {
def run = Files[IO].currentWorkingDirectory.flatMap { cwd =>
IO.println(cwd.toString)
}
}
- accessibility of the language
- ready pathways to follow
- standardizes on approaches
- gives you the right tools for the task
cross version suffix
toolkit still in early phases, we need to add more libraries!
coordinates are quite long `org:name:version`
directives in the comment seem easy to break despite being very useful
no automatic completion of the directives, one must know about them
set additional corporate repositories to download Metals artifacts from
coursier mirror if it’s not possible to download from Maven central directly
setting up proxies for both Metals and Bloop to be done in user settings
Errors are a way of communicating with the user, one of the first thing they will see.
class A
val i: Int = A()
> scala compile .
Compiling project (Scala 3.3.0, JVM)
[error] ./main.scala:5:14
[error] Found: A
[error] Required: Int
[error] val i: Int = A()
[error] ^^^
Error compiling project (Scala 3.3.0, JVM)
Compilation failed
-- [E007] Type Mismatch Error: /home/tgodzik/Documents/workspaces/sample-scala-cli-project/main.scala:6:14
6 |val i: Int = A()
| ^^^
| Found: A
| Required: Int
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
| Tree: new A()
| I tried to show that
| A
| conforms to
| Int
| but the comparison trace ended with `false`:
|
| ==> A <: Int
| <== A <: Int = false
|
| The tests were made under the empty constraint
-----------------------------------------------------------------------------
1 error found
Compilation failed
Compiler or build tools know how to fix their stuff (most of the time)
We can propagate automatic fixes suggestions form the compiler to the IDEs
This gives us errors with ready fixes for the user to try
there is still plenty of work to do on making the compiler messages clearer
–explain option requires rerunning the compilation and doesn't work with Bloop
the actionable diagnostic journey has only begun, there is plenty of work to be done
Automatic changes to the code driven by the IDE
Expected by more advanced users especially when coming from other languages
Affects the maintainability of the software we create
Well known for large amount of refactorings (aside maybe from Scala 3 issues)
Still catching up and might never fully provide the same capabilities
Scalafix needs more work with the API and Scala 3 support
custom rules need to be added by hand to metals configuration
refactorings in Metals are an ongoing work and it’s quite hard to handle all the edge cases especially in a language like Scala