As a brief note today, here’s an example of the Scala 3 “Dotty” if/then/else-if/else syntax, as used in a function:
def compare(a: Int, b: Int): Int = if a < b -1 else if a == b 0 else 1
Using `then` with if/else
You can also use the
then keyword after your
if expressions, if you prefer:
As a note to self, when you see a Scala error message that looks like this:
it probably means that you have a mismatch in the Scala versions you’re using in your project. For instance, I just tried to use a library I compiled with Scala 2.12 with Spark, which was compiled with Scala 2.11, and I got that error message. In this case I was able to resolve the problem by recompiling my library with Scala 2.11.
I had read that Bloop was faster than Scala compiler tools like
fsc, so I wondered if it was faster than SBT, and if so, how much faster. So I downloaded Eric Torreborre’s specs2 source code, which has 880 source code files, and compiled the entire project with both SBT and Bloop.
To test SBT’s speed, I ran all the commands from inside the SBT command prompt, which I usually do anyway to avoid the SBT/JVM startup lag time. I also ran
compile several times before recording SBT’s speed, because I thought that would be a better reflection of real-world usage and performance. I ran the tests four times, and the average time with SBT was 49 seconds, and that was very consistent, always coming in between 48 and 50 seconds.
The people at Triplequote looked at the performance of Scala compilers on different Amazon EC2 instance types in an effort to answer the question, “What is the (cloud) cost per build?”
From the URL:
Q: Scala makes a big deal about how what seem to be language features are implemented as library features. Is there a list of types that are treated specially by the language?
A: The following types are crucial to Scala's type system. They have an influence on how type checking itself is performed.
It’s interesting that you can do some research on this by looking at Definitions.scala.
As a quick note to self, here’s an example of how to set
scalacOptions in an SBT build.sbt file:
scalacOptions ++= Seq( "-Xfatal-warnings", "-deprecation", "-feature", "-unchecked", "-language:implicitConversions", "-language:higherKinds", "-language:existentials", "-language:postfixOps" )
scalacOptions lets you set Scala compiler options in your SBT project build.
If you like reading PDFs of presentations, here’s a PDF from a Twitter employee named How We Built Tools That Scale to Millions of Lines of Code.
scala-lang.org has an article titled Speeding Up Compilation Time with `scalac-profiling` where they demonstrate how they reduced a project’s compilation time from 32.5 seconds down to 4 seconds. In addition to all of the scalac and profiling details, it demonstrates a nice use of flamegraphs.