Posts in the “scala” category

ZIO 2: Error-Handling Decision Tree (Flowchart)

This page provides a comprehensive overview of error-handling strategies in ZIO 2. My hope is that you can use this decision tree to determine good/best approaches for handling errors in your ZIO effects. Each section includes a use case (question/answer), brief explanation, and ZIO 2 example.

As a brief note, I have ensured that the following examples compile, but I feel like I need to double-check some of my work.

Scala: What is the Nothing data type?

Scala FAQ: What is the Nothing type in Scala, and how do I use it?

Solution

In Scala, the Nothing type is called a bottom type, which means it is a sub-type of every other type in the Scala type system. It is also specifically a data type that has no instances.

In practical use, Nothing is used to indicate that a computation or function will never produce a result normally, either because it throws an exception, enters an infinite loop, or encounters some other abnormal termination.

Visually, this is what the Nothing type looks like in the Scala type hierarchy (image courtesy of this scala-lang.org page):

The Scala type hierarchy and the Nothing data type

Scala: Common uses of Nothing

Some common use cases of Nothing in Scala include:

The ZIO 2 “mental model”

As I work more with ZIO 2, I also find myself thinking a lot about the ZIO 2 mental model, by which I partially mean “a way of thinking about your code” and also “ZIO 2 best practices.”

Here are my initial notes. Also, I hope that most of this code is mine, but several snippets are from other sources that I don’t remember, including the official ZIO 2 docs.

Yvonne De Carlo in The Ten Commandments (and more)

I recently watched the movie, The Ten Commandments, and when I saw Sephora, I thought, “She looks familiar.”

It turns out she’s played by Yvonne De Carlo. She’s the actress who’s portrayed on my Learn Scala 3 book cover, which is based on the 1950 movie, Buccaneer’s Girl, which she starred in. (Until this, I thought I only knew her as Lily Munster on The Munsters.)

And if you like the movie The Ten Commandments, I also wrote this blog post about The Ten Commandments and the 2024 Presidential Election.

Scala: How to get the first match in a sequence and immediately return

As a quick note to self, I wrote this Scala code as a way to (a) find the first element in a sequence, and then (b) return that element without traversing the rest of the sequence.

I initially thought about writing this code with a while loop, for loop, or for expression — because I knew I needed a loop and a way to break out of a loop — but then I realized that an iterator would help me out here.

Also, please note that there is a potentially-better solution after this — the one that uses the “view.”

A first solution

So without any further ado, here’s this solution:

Scala best practice: Think “Expression-Oriented Programming”

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 20.3, “Scala best practice: Think "Expression-Oriented Programming".”

Problem

You’re used to writing statements in another programming language, and want to learn how to write expressions in Scala, and the benefits of the Expression-Oriented Programming (EOP) philosophy.

[toc hidden:1]

Scala 3: Functions: Options and Functional Error Handling (Part 1)

NOTE: This is a chapter from my book, Learn Scala 3 The Fast Way. Due to a mistake, this lesson was not included in the book.

When you write functions, things can go wrong. In other languages you might throw exceptions or return null values, but in Scala you don’t do those things. (Technically you can, but other people won’t be happy with your code.)

Instead what we do is work with error-handling data types. To demonstrate this I’ll create a function to convert a String to an Int.

Scala 3 Unions: Simulating Dynamic Typing with Union Types

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.9, Simulating Dynamic Typing with Union Types.

Problem

When using Scala 3, you have a situation where it would be helpful if a value could represent one of several different types, without requiring those types to be part of a class hierarchy. Because the types aren’t part of a class hierarchy, you’re essentially declaring them in a dynamic way, even though Scala is a statically-typed language.

How to read “difficult” Scala method type signatures

When I first saw Scala generic type and multiple-parameter group code like this ~12 years ago, my initial thought was, “Wow, maybe I need to think about a different career”:

def race[A, B](lh: IO[A], rh: IO[B])(implicit cs: ContextShift[IO]): IO[Either[A, B]]

But in the end, as Rocky once said, it ain’t so bad. :)

Rocky: You ain’t so bad

What is “Functional Programming”?

Note: This is an excerpt from my book, Functional Programming, Simplified. (No one has reviewed this text yet. All mistakes are definitely my own.)

Defining “Functional Programming”

It’s surprisingly hard to find a consistent definition of functional programming. As just one example, some people say that functional programming (FP) is about writing pure functions — which is a good start — but then they add something else like, “The programming language must be lazy.” Really? Does a programming language really have to be lazy (non-strict) to be FP? (The correct answer is “no.”)

I share links to many definitions at the end of this lesson, but I think you can define FP with just two statements:

Scala: How to use higher-order functions (HOFs) with Option (instead of match expressions)

I originally wrote a long introduction to this tutorial about how to work with the Scala Option/Some/None classes, but I decided to keep that introduction for a future article. For this article I’ll just say:

  • idiomatic Scala code involves never using null values
  • because you never use nulls, it’s important for you to become an expert at using Option, Some, and None
  • initially you may want to use match expressions to handle Option values
  • as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
  • becoming proficient with higher-order functions (HOFs) like map, filter, fold, and many others are the cure for that verbosity

Given that background, the purpose of this article is to show how to use HOFs rather than match expressions when working with Option values.