Posts in the “scala” category

Scala money and currency: The BigDecimal class and libraries

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.

A Scala shell script example (and discussion)

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:

Scala: What is the Nothing data type?

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):

The Scala type hierarchy and the Nothing data type

Scala: Common uses of Nothing

Some common use cases of Nothing in Scala include:

Scala: How to square a number (Int, Double, Float, Long)

[toc]

Scala math FAQ: How do I square a number in Scala, such as squaring an Int, Double, Long, or Float?

Solution

You can square a number in Scala in at least two different ways:

  1. Multiply the number by itself
  2. Call the Java Math.pow function or the scala.math.pow function

My free “Advanced Scala 3” video training course

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.>>

Scala: How to use startsWith tests in match/case expression

Scala FAQ: Can I use the startsWith method on a Scala String to match multiple possible patterns in a match expression?

Solution

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.

Example: startsWith + match expression

Scala 3: An apply/factory method that takes a varargs/tuple parameter

Here’s a brief Scala 3 example that shows how to:

  1. Create a companion object,
  2. Create an apply method in that companion object that acts as a factory method,
  3. Define that apply method to take a varargs tuple parameter, and
  4. Create new Person instances using that factory method.

Here’s the complete source code for this example:

Scala 3 dates: How to calculate the difference between two dates (LocalDate, ChronoUnit)

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.10, Calculating The Difference Between Two Dates.

Problem

While using Scala (Scala 2 or 3), you need to determine the difference between two dates.

Solution: Difference between two dates (in Scala and Java)

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:

Scala 3 dates: How to parse strings into dates (LocalDate, DateTimeFormatter)

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.12, Parsing Strings Into Dates.

Problem

While using Scala (Scala 2 or 3), you need to parse a String into one of the date/time types introduced in Java 8.

Solution

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.

Scala 3: How to Create New Date and Time Instances

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.9, Creating New Date and Time Instances.

Problem

In Scala, you need to create new date and time instances using the Date and Time API that was introduced with Java 8.

Solution: Dates and times in Scala (and Java)

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.