scala

Tutorials about the Scala programming language.

How to create an executable JAR file with `scalac` (and run it with `scala`)

Table of Contents1 - Example2 - A more complex example3 - Summary

If you’re ever working on a really small Scala project — something that contains only a few source code files — and don’t want to use SBT to create a JAR file, you can do it yourself manually. Let’s look at a quick example. Note that the commands below work on Mac and Linux systems, and should work on Windows with minor changes.

Scala shell scripts hang indefinitely on MacOS 10.13 and 10.14 (won’t run) alvin April 15, 2019 - 6:37pm

If you run into a problem where a Scala shell script won’t run on MacOS — it hangs indefinitely without doing anything — hopefully this bug report will help. The solution is to change this line at the beginning of the Scala shell script:

exec scala -savecompiled "$0" "$@"

to this:

exec scala -nocompdaemon -savecompiled "$0" "$@"

I just had this problem with Scala 2.12.x and Java 8 running on MacOS 10.14.4, and I can confirm that adding -nocompdaemon solved the problem for me.

What def, val, and var fields in Scala traits look like after they’re compiled (including the classes that extend them)

Table of Contents1 - def field in trait2 - val field in trait (abstract)3 - val field in trait (concrete)4 - var field in trait (abstract)5 - var field in trait (concrete)6 - An abstract class in the middle7 - A trait in the middle8 - Summary

I generally have a pretty good feel for how Scala traits work, and how they can be used for different needs. As one example, a few years ago I learned that it’s best to define abstract fields in traits using def. But there are still a few things I wonder about.

Today I had a few free moments and I decided to look at what happens under the covers when you use def, val, and var fields in traits, and then mix-in or extend those traits with classes. So I created some examples, compiled them with scalac -Xprint:all, and then decompiled them with JAD to see what everything looks like under the covers.

I was initially going to write a summary here, but if you want to know how things work under the hood, I think it helps to work through the examples, so for today I’ll leave that as an exercise for the reader.

An example of stackable modifications in Scala

As a brief note today, here’s an example of stackable modifications in Scala.

Lately I was curious about what super means when you mix Scala traits into a class or object. A simplified answer is that super refers to the last trait that’s mixed in, though I should be careful and note that this is an oversimplification.

This can be demonstrated in an example that uses both inheritance and mixins with traits. Given this combination of traits and classes:

Hello, Scala (free preview)

Note: The print version of Hello, Scala on Amazon will be going up to $20 (USD) this Saturday, April 13, 2019. It’s currently just $10, so you know, buy it while it’s on sale and all that. :)

If you like free things, here’s a link to a free preview (PDF) of the new version of my book, “Hello, Scala.” The book is 257 pages long, and the free preview contains the first 120 pages of it, so I hope it’s a significant preview. Hello, Scala is now available for sale, so you can buy the PDF, Kindle ebook, and/or printed book at this link.

How to left-justify a multiline Scala string

If you ever need to left-justify a multiline Scala string, the solution is to use the stripMargin method, as shown in this example:

def getWordFormatted(s: String): String = {
    s"""
      |
      |Word of the Day
      |---------------
      |Word: $s
    """.stripMargin
}

The | character has a special meaning when used in conjunction with stripMargin, so that method results in a string like this being output:

Scala/Dotty SIP process: A better `main` approach? alvin April 1, 2019 - 6:26pm

contributors.scala-lang.org remains my favorite website of late. This pre-SIP discussion looking for better ways to define a `main` method is a fun read that demonstrates a collaborative process.