Posts in the “scala” category

Scala 3 opaque types: How to create meaningful type names

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.7, Creating Meaningful Scala Type Names with Opaque Types.

Problem

In keeping with practices like Domain-Driven Design (DDD), you want to give values that have simple types like String and Int more meaningful type names to make your code safer and easier to read.

Solution

In Scala 3, the solution is to use opaque types to create meaningful type names. For an example of the problem, when a customer orders something on an e-commerce website you may add it to a cart using the customerId and the productId:

ZIO 2: Passing a ZLayer value to an application, getting a return value, and handling possible errors

As a little note today, here’s an example ZIO 2 application where I do the following:

  • Pass a value to the ZIO application (named app) from run using a ZLayer
  • Use that ZLayer value in the application
  • Pass a result value from the application back to the run value
  • Use that value in run
  • Handle any possible errors in run with foldZIO
  • Show other ways to write the run value/function

ZIO/ZLayer example

Given that introduction, here’s the ZIO source code:

ZIO/ZLayer FAQ: How to use a Java Properties files with ZIO

ZIO/ZLayer FAQ: How do I use a Java Properties file with ZIO 2 and Scala?

Solution

You can use the zio-config library for things like this, but at the moment my preferred approach is to hand-code this ZLayer solution. Maybe that’s because I know how to work with a Java Properties file — i.e., how to read and load it — so I like to see those details.

Therefore, given this Java properties file named application.properties:

A ZIO ZLayer logging example (with Scala-CLI)

As a brief note today, and without much discussion, here’s a ZIO 2 ZLayer example I’ve been working on. The intent of the example is to show one possible way you can enable logging in a ZIO application using ZLayer.

ZIO ZLayer and logging example

As I mentioned, without much discussion, here’s the ZIO 2 source code:

Scala examples: Reading HOCON configuration files, and screen-scraping with JSoup and Sttp

If you’re interested in this sort of thing, I’ve been trying to fix the pages for my free Scala and functional programming video courses, and to that end I needed to do some things with reading HOCON configuration files, and screen-scraping of my own website pages.

Therefore, without much explanation, here is some source code that I wrote over the last day or two, with the help of an A.I. tool or two. One thing to note is that the quality of the code isn’t very good, because I let the A.I. tools generate most of it, and I didn’t bother to clean it up.

Free “Introduction to Functional Programming” video training course: January 21, 2024

My free “Introduction to Functional Programming” video course is almost complete. Today I released 12 new lessons, starting with for Loops, Generators, and Guards, and ending with Lessons Learned.

After this there are about five final lessons (depending on what I decide to include), and by the end of the course we’ll start writing some ZIO code.

As always, many thanks to Ziverge’s Scala and Rust consulting services for sponsoring the creation of these “100% Free” videos, with no ads and no firewalls!

Scala performance FAQ: Is there a way to determine the number of processors, CPUs, or cores on a computer?

Scala performance FAQ: Is there a reliable way to determine the number of processors (CPUs) or cores when writing Scala code?

Solution

As I was reading this article on ZIO performance tuning, I decided to look into whether there is a reliable way to determine the number of CPUs or cores on a computer using Scala (and therefore Java, Kotlin, and other JVM languages).

The solution is that there seems to be a reliable way to determine the number of CPUs or cores in Scala. In short, use the Java Runtime class to get the information. This Scala example shows how to do it:

ZIO 2 Example: Print values after a random delay (and the ZIO error channel)

As a brief note today, here’s a little ZIO 2 example that shows how to print a series of values with a random delay in between each value that’s printed.

Also note that in the code below there are different ways to implement randomWaitTimeInSeconds ... for instance, it could return a Duration, but I just have it return an Int.

I also use ZIO.foreach to generate the values in a range, and that could be handled differently.

Another thing I do is use an exception inside ZIO.fail, and I do that because I want that error to be a Throwable on the ZIO “error channel” (i.e., the E parameter in ZIO[R, E, A].)

ZIO.attempt: examples and documentation

ZIO 2 FAQ: How do I work with the ZIO.attempt function?

Solution

When you’re working with existing (legacy) Scala code that:

  • is synchronous, and
  • can throw an exception

wrap that code with ZIO.attempt. As you’re about to see, this creates a ZIO effect from that legacy code.

Not only does this create a ZIO effect, it also puts the exception in the ZIO “error channel,” i.e., the E parameter in the ZIO[R, E, A] type signature.