Posts in the “scala” category

My free “Advanced Scala 3” video training course

March 24, 2024: I just released my free “Advanced Scala 3” online video training course. This free video course gets into different Scala programming topics such as functions, types, generics with variance and bounds, multiversal equality, modular programming, extension methods, and much more.

As always I want to thank Ziverge’s software consulting services for sponsoring these videos! These video courses take many weeks and even months to create, and they would not exist without Ziverge.

<<Click here to start my free Advanced Scala 3 video training course.>>

Free functional programming book (free PDF for Scala, Java, Kotlin, etc.)

April, 2024: As a brief note today, the PDF version of my book, Learn Functional Programming The Fast Way!, is now FREE. I wrote this functional programming book for Scala, Java, and Kotlin developers, and you can now download it for free here:

If you’re interested in functional programming, or just want to learn more about data types, generics, pure functions, expression-oriented programming, and functional error handling, I hope this book is helpful.

Scala SBT: How to “re-run with -deprecation” (or -feature)

Scala FAQ: When compiling a Scala application with SBT, I get warning messages like these:

$ sbt compile

[warn] there were 6 deprecation warnings; re-run with -deprecation for details
[warn] there were 4 feature warnings; re-run with -feature for details

How do I ’re-run with -deprecation’ or ’re-run with -feature’?

A Scala Either, Left, and Right example (like Option, Some, and None)

Summary: This post is a discussion of the “Option/Some/None Pattern” in Scala, specifically how to use the Either/Left/Right data types instead of Option when you need to know the reason some code failed. As you may know, the None data type does not return failure/exception information, but if you use the Either/Left/Right types, you can access that failure information through the Left type.

The post is sponsored by my book, the 2nd Edition of the Scala Cookbook.

Scala List class examples: range, fill, tabulate, appending, foreach, more ...

Scala List FAQ: Can you share some Scala List class examples?

The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.

In this tutorial, I'll share examples of the most common List operations (methods).

Creating a Thread (and Runnable) in Scala

I ran into a strange problem this weekend where I noticed a huge difference between the way a Scala Future and a Thread worked for the exact same code. I think I’m pretty aware of the obvious differences between futures and threads, so this really caught me by surprise. If/when I figure out why there was such a difference in behavior for the given problem I’ll post that information here.

A Scala Thread example

While that problem will haunt me for a while, what I really want to post here today is a simple Scala Thread example:

How to convert a Scala Array/List/Seq (sequence) to string with mkString

Scala collections FAQ: How can I convert a Scala array to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.)

A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)

Using the Scala Option, Some, and None idiom (instead of Java null)

A powerful Scala idiom is to use the Option class when returning a value from a function that can be null. Simply stated, instead of returning one object when a function succeeds and null when it fails, your function should instead return an instance of an Option, where the instance is either:

  1. In the success case, return an instance of the Scala Some class
  2. In the failure case, return an instance of the Scala None class

Because Some and None are both children of Option, your function signature just declares that you're returning an Option that contains some type (such as the Int type shown below). At the very least, this has the tremendous benefit of letting the user of your function know what’s going on.

Scala 3: An apply/factory method that takes a varargs/tuple parameter

Here’s a brief Scala 3 example that shows how to:

  1. Create a companion object,
  2. Create an apply method in that companion object that acts as a factory method,
  3. Define that apply method to take a varargs tuple parameter, and
  4. Create new Person instances using that factory method.

Here’s the complete source code for this example: