Beginner's guide to toolkits in Scala
So you are learning Scala and see all the news about scala-cli and it becoming the new scala runner, Scala Toolkit, Typelevel Toolkit and things become a little confusing?
This post is here to help you understand what’s going on. To make some sense of it, let’s tell a bit of a story.
💻 Scala runner and scala-cli
First of all, what’s Scala runner? basically it’s the program that you launch by typing scala
in your terminal. It can launch a basic REPL or run provided Scala file, but not much more.
Is it a problem anyway? In Scala Survey 2022 50.8% of the respondents pointed out that tooling is the thing they would like to see improved.
One of the things that hurt me the most is the lack of the possibility of using libraries with your code. In Python you just install things globally with pip
and just import the library in your code. This is super useful when you want to play with the language without setting up a full blown project. With the current runner you’d need to set up an SBT/Mill project and this is in my opinion an unnecessary difficulty for the beginners.
This is where scala-cli comes into play - it aims to solve this and many other problems. The important part of the solution are using
directives. They let you configure things like Scala version, compiler flags and add dependencies. This is a great step towards making Scala more approachable for beginners.
If you are looking for the full story check out the pre-SIP https://contributors.scala-lang.org/t/pre-sip-scala-cli-as-new-scala-command/5628 and SIP-46. The later is now in the implementation phase. As soon as that’s done, scala-cli
will replace the current scala
so stay tuned!
🧰 Toolkits
Scala Toolkit
While scala-cli
is a vey convenient tool, it doesn’t stop there. Scala Center and VirtusLab have started the Scala Toolkit initiative (https://virtuslab.com/blog/scala-toolkit-makes-scala-powerful-straight-out-of-the-box/). The aim is straightforward. Going back to the analogy of Python - people would usually use the repl for simple tasks without worrying about the dependencies because they either have the most popular libraries pre-installed already, or have the functionality in the standard library.
The solution in scala world is to put a single dependency like this:
//> using toolkit "latest"
This brings in the set of opinionated libraries like sttp (HTTP client), MUnit (for testing), UPickle/UJson (data serialization) and OS-Lib (for working with operating system).
Typelevel Toolkit
Scala-cli allows you to bring in a dependency, and thus transitive dependencies. This makes creating your own toolkit is as simple as creating a project tht has no own code and adding a set of your favorite libraries as the dependencies.
This is what Typelevel and this is how Typelevel Toolkit https://github.com/typelevel/toolkit was created. It’s a similar thing to Scala Toolkit, just that it brings other libraries to the table - in this case things like Cats, Cats Effect, FS2 and others.