Posts in the “scala” category

Functional Programming FAQ: What are the benefits of an Effect System, like ZIO?

Functional Programming FAQ: What are the benefits of an Effect System, like ZIO?

Answer

I’m currently planning a video on “The benefits of an Effect System in computer programming” on my free Scala and functional programming videos website, but in case I don’t get around to making that video, here are the slides I have put together.

Also, if you’d like to see a video that is somewhat similar, here’s my popular “What is ZIO 2?” video on YouTube.

Background

If you haven’t heard of Effects or an Effects System, I wrote about them a long time ago here. Also, in book, Effect-Oriented Programming, the authors describe them this way:

  • Effects are the unpredictable parts of a system.
  • Effect Systems partition the unpredictable parts, and manage them separately from the predictable ones.

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.