Scala 3 Opaque Types: When to use, examples, benefits
I previously wrote 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.
I previously wrote 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.
Scala date/time FAQ: How do I get the current date and time in Scala?
The following code demonstrates how to get the current time in Scala, and then further shows how to get other information, such as the current minute, using the Java SimpleDateFormat class:
Note: I don't have any immediate solutions in this article; it's more of a discussion of where I'm at today when looking at handling money/currency in Scala.
As a quick note, I've started to look at handling money/currency in Scala, and I'm also starting to explore a couple of money/currency libraries.
I've been trying to find some good Scala currency and money libraries lately, and I just ran across the following list of projects on the Joda Money Github project. That URL contains the following list of Java and JVM-based projects that should all be usable in Scala:
Scala shell script FAQ: How do I create a Unix/Linux shell script to run a small Scala script?
If you want to run a Scala script as a Unix or Linux shell script -- such as hello.sh -- write your script like this:
March 26, 2024 Update: As a brief note today, here are links to my new, free online video training courses:
As always, these videos are sponsored by Ziverge software consulting (specializing in Rust, Scala, and ZIO).
March 24, 2024: Here’s a summary of my current free online Scala and functional programming training courses:
If you’re interested in 100% Free online Scala and FP video training courses, I hope these online video courses are helpful.
Here are links to my free Scala and functional programming training videos:
These online video courses are “100% Free”, meaning that there are no ads and no paywalls — they are completely free to view.
As I create more free Scala and FP video training courses, I’ll add links to them here.
In Scala, Nothing
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:
[toc]
Scala math FAQ: How do I square a number in Scala, such as squaring an Int
, Double
, Long
, or Float
?
You can square a number in Scala in at least two different ways:
Math.pow
function or the scala.math.pow
functionIf you’d ever like to pay it forward — or in this case, backwards — all of my free Scala and functional programming video training courses are sponsored by Ziverge, and if you ever need to supplement your development team, please see their On-Demand Team Extension Service. They have qualified developers that work with Scala, Rust, artificial intelligence, and many other technologies.
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.>>
My 100% FREE “Introduction to Functional Programming” online video training course is now finished! To make things a little easier, here are links to all of the free videos in the training course:
Scala FAQ: Can I use the startsWith
method on a Scala String
to match multiple possible patterns in a match
expression?
Yes, as shown in the following example, you can use the startsWith
method on a String
to match multiple possible patterns in a match
expression. startsWith
checks to see if a String
starts with the prefix (or substring) you specify.
Scala FAQ: How do I perform pattern matching on a regular expression string in a match
expression?
Using Scala 3, I was just trying to perform some pattern matching on a regular expression (regex) string in a match
expression, and while working with ChatGPT, I came up with this solution, which you can easily verify in the Scala REPL:
Here’s a brief Scala 3 example that shows how to:
apply
method in that companion object that acts as a factory method,apply
method to take a varargs tuple parameter, andPerson
instances using that factory method.Here’s the complete source code for this example:
This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.10, Calculating The Difference Between Two Dates.
While using Scala (Scala 2 or 3), you need to determine the difference between two dates.
If you need to determine the number of days between two dates in Scala (or Java), the DAYS
enum constant of the java.time.temporal.ChronoUnit class is the easiest solution:
This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.12, Parsing Strings Into Dates.
While using Scala (Scala 2 or 3), you need to parse a String
into one of the date/time types introduced in Java 8.
If your String
is already in the expected format, pass it to the parse
method of the desired class. If the String
is not in the expected (default) format, create a formatter to define the format you want to accept. The following examples demonstrate the expected formats, and other solutions.
This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.11, Formatting Dates in Scala.
While using Scala (2 or 3), you want to format dates, such as for printing them out in a desired format.
The solution is to use the java.time.format.DateTimeFormatter class. It provides three types of formatters for printing date/time values:
This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.9, Creating New Date and Time Instances.
In Scala, you need to create new date and time instances using the Date and Time API that was introduced with Java 8.
Using the Java 8 API and newer, you can create new dates, times, and date/time values. Descriptions of common Java 8 Date and Time classes provides a description of some of the new classes you’ll use (from the java.time Javadoc), all of which work in the ISO-8601 calendar system.