Why I like Scala
Scala is a programming language that combines object-oriented and functional programming and static types. Scala is used at Twitter and Netflix.
I write a game engine in Scala. I wanted to share some things that I like about Scala:
1. Scala’s standard library explicitly differentiates between mutable and immutable collections. This distinction permeates the language and library ecosystem. After using Scala for a couple years, this is such an obviously good and useful idea. I miss it in other languages.
2. When writing software it’s always necessary to take the program design that you see your brain and convert it into code. Scala makes this easier than any other language I’ve ever used. Scala has a lot of features that allow you to “actually express your intent” instead of shoe-horning your mental design into the language. One of my favorite examples of this is Scala’s feature of “self-types”. Here is a demo.
3. Scala’s library ecosystem is a “bazaar” not a “cathedral”. Languages like golang will get you very far with the standard library. Scala’s standard library is excellent, however it is intentionally incomplete. Whereas you can write a pretty wholistic, idiomatic golang program with zero dependencies, Scala is, more or less, the opposite — a lot of key functionality is provided by competing 3rd party libraries. This is something I didn’t understand for a long time (“why is there no monoid in the standard library?”) but now I see that this “market-based economy” of 3rd party libraries is a core strength of Scala. Definitely keep a short-list of excellent libraries. Here is my list of Scala libraries. The most important library to know is cats, and to know that scalaz is a somewhat-competitor to cats. I’d recommend using cats. Two other important libraries are Monocle and circe.
4. Scala is a fusion of object-oriented and functional programming. Most of my game engine code is non-functional. But, Scala’s deep functional ecosystem is there for me if desired. My in-game world editor uses a layer of pure functions, with the Monocle library, which gives me the safety and power of the functional paradigm in an area of the codebase that doesn’t need extreme performance. An example of this power is I can easily roll back content changes by keeping references to the old content objects because they’re immutable.
5. Scala has first-class support for the typeclass pattern. Typeclasses are a solution to the expression problem which means that they are a (vastly) superior way of making your data structures and algorithms extensible. Usually I expect that various approaches have pros and cons. Typeclasses don’t really have any cons after the learning curve, they are just a Better Way(TM).
6. Scala can be deployed in-browser using the excellent scala-js project.
8. Scala 3 is coming and is looking really awesome! Here is a recent, great overview.
I’d recommend Scala for general backend development, to write command-line apps with fast startup from native compilation via graalvm, and to publish TypeScript libraries for the frontend via scala-js.
- Official Scala Website
- Scala subreddit
- Scastie or ScalaFiddle to run & share little Scala programs directly in your browser
- Scala Times, the weekly Scala newsletter
- Li Haoyi’s excellent Scala blog. I love this post on the Principle of Least Power
- Unofficial Scala Discord
- Try Scala on OSX with
brew install scala sbt. Make a new project with
sbt new scala/scala-seed.g8. Then
sbt runin the directory it creates. Try launching sbt interactively with
~runto watch for changes as you play with