How to debug a ZIO HTTP Request value (a debug function)
As a brief note, if you need to debug a ZIO HTTP Request
value, I just created this function to return its values as a String
, and it appears to work properly:
As a brief note, if you need to debug a ZIO HTTP Request
value, I just created this function to return its values as a String
, and it appears to work properly:
As a brief note, my Mac/macOS system disk was pretty full, so I started looking at things I could delete easily. One thing I found is that the Scala-CLI/Coursier cache is location in this Mac directory:
/Users/al/Library/Caches/Coursier
I don’t know yet if it’s safe to delete anything in that directory/folder, but it’s using 7.2GB of space, so I’m about to look into it.
UPDATE: Here is information on deleting the scala-cli/Coursier cache.
Note that I deleted everything in that Coursier directory, and after that NONE of my Java and Scala tools worked, and I had to manually run commands like this to get Java working again:
eval "$(cs java --jvm 11 --env)"
and then later I had problems with scala-cli
and had to run this command to kill bloop
:
scala-cli --power bloop exit
In case these two examples disappear from the ZIO JDBC repository, I have made a copy of them here.
(I say that because earlier this week (late October, 2024) I was told the the ZIO JDBC library is no longer maintained, and then I was also told that they just found a new maintainer.)
That being said, here are the ZIO-JDBC examples. This first one is called Basic.scala
:
Here’s a brief Scala ZIO HTTP “Hello, world” example from that repository, followed by a description of the code and “key points” to know about the example:
import zio.*
import zio.http.*
object HelloWorld extends ZIOAppDefault:
// responds with plain text
val homeRoute =
Method.GET / Root -> handler(Response.text("Hello, World!"))
// responds with JSON
val jsonRoute =
Method.GET / "json" -> handler(Response.json("""{"greetings": "Hello World!"}"""))
// create HTTP route
val app = Routes(homeRoute, jsonRoute)
// run it like any simple app
override val run = Server.serve(app)
.provide(Server.default)
I just added free videos on ZIO 2 fibers and parallel/concurrent combinator functions to my Scala/FP video training course website.
Just a periodic reminder today that here’s a link to my free Scala and functional programming training video courses at LearnScala.dev.
If you haven’t seen them yet, here’s a link to Alvin Alexander’s free Scala 3 and Functional Programming video courses. The functional programming course shown in this image was just released today, September, 8, 2024.
October, 2024, and my new free “Functional Programming Fundamentals with ZIO” video course is now available!
Functional Programming FAQ: What are the benefits of an Effect System, like ZIO?
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.
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:
In this fast-paced video I show what the ZIO 2 library is in the fastest way I know how. I show the benefits of effects and effect systems, and specifically how ZIO is the new “Functional Programming, Simplified.”
I noticed that the ZIO 2 ZIO.fromOption
method returns Option[Nothing]
as its error type, so I asked my friend Claude about it, and got the following response, which I have cleaned up a little.
Scala FAQ: What is the Nothing
type in Scala, and how do I use it?
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):
Some common use cases of Nothing
in Scala include:
Functional Programming, Simplified — currently 5-star rated on Gumroad.com, 4.5-star rated on Amazon, and one of the all-time best-selling books on functional programming — is currently on sale in three formats (prices shown in USD):
PDF Format |
Paperback Book |
Kindle eBook |
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.
I’m glad to report that my book, Learn Functional Programming The Fast Way, is five-star rated on Gumroad.com. And as I write this sentence on September 6, 2024, it’s also FREE!
Scala 3 FAQ: What are opaque types in Scala?
I previously wrote a little about Opaque Types in Scala 3, and today, as I’m working on a new video about opaque types, I thought I’d add some more information about them.
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.”
So without any further ado, here’s this solution:
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".”
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.
Because functional programming is like algebra, there are no null values or exceptions in FP code. But of course you can still have exceptions when you try to access servers that are down or files that are missing, so what can you do? This lesson demonstrates the techniques of functional error handling in Scala.
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
.