scala

Tutorials about the Scala programming language.

SBT: 'compiler-interface' not yet compiled for Scala 2.12.2. Compiling... alvin August 18, 2017 - 6:34pm

SBT FAQ: What does the message, “'compiler-interface' not yet compiled for Scala 2.12.2. Compiling...” mean?

Answer: I don’t know all of the nitty-gritty details, but the short version of the answer is described in the SBT docs:

SBT: How to pass command line arguments to ‘sbt run’ alvin August 17, 2017 - 11:30am

Question: How do I pass command-line parameters to my Scala application when I’m running the application with SBT?

Solution: There are two different possible scenarios here:

Next version of Learning Functional Programming in Scala alvin August 16, 2017 - 10:25am

I get to have another operation (surgery) next week, but I still hope to have the next version of my book on Scala and functional programming available by the end of the month.

SBT 1.0 is now available alvin August 11, 2017 - 8:01am

SBT 1.0.0 is now available. See this Lightbend page for more details.

Scala: How to define a generic method parameter that must extend a base type alvin August 9, 2017 - 7:09pm

In today’s installation of “how to have fun with Scala,” if you want to define a method that takes a parameter that has a generic type, and want to further declare that the parameter must extend some base type, use this syntax:

def getName[A <: RequiredBaseType](a: A) = ???

That example says, “The parameter a has the generic type A, and A must be a subtype of RequiredBaseType.”

Scala for Java devs: Everything in Scala is an object alvin August 9, 2017 - 11:26am

The new scala-lang.org docs website looks great. It’s also a reminder to me that I probably didn’t stress enough in the Scala Cookbook that everything in Scala is an object, including numbers. (Hopefully I made it clear that functions are objects.) This Scala REPL example shows some of the methods that are available on Scala integers (Int type).

Another Scala nested Option + flatMap example alvin August 8, 2017 - 1:35pm

After yesterday’s Scala nested Option + flatMap/for example, here’s another example of plowing through nested Options with flatMap. First, start with some nested options:

val o1 = Option(1)
val oo1 = Option(o1)
val ooo1 = Option(oo1)

Here are those same three lines, with the data type for each instance shown in the comments:

Scala: Handling nested Options with flatMap and for alvin August 7, 2017 - 5:47pm

Summary: In this article I show a couple of ways to extract information from optional fields in your Scala domain models. This example is a little contrived, but if you have a situation where one Option instance contains one or more other Options, this article may be helpful.

There are times when you’re creating your domain model when it makes sense to use optional fields in your case classes. For instance, when you model an Address, the “second street address” isn’t needed for all people, so making it an optional field makes sense:

A collection of 100+ Scala String examples alvin August 7, 2017 - 11:35am
Table of Contents1 - String equality2 - Multiline strings3 - String interpolation/substitution4 - Substrings5 - Treating a String as an Array[Char]6 - Using map and for7 - Regular expressions8 - Transforming arrays to a String9 - A lot of String method examples

This page contains a collection of over 100 Scala String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)

First, here are some basic uses of the Scala String class to help get us warmed up:

“Pure functional programming is programming with mathematical functions” alvin August 6, 2017 - 2:13pm

“Pure functional programming is programming with mathematical functions.”

~ Erik Meijer