statement

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.

How to hide a class (or classes) with Scala import statements

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 7.4, “How to hide a class (or classes) with Scala import statements.”

Problem

You want to hide one or more classes while importing other members from the same package.

Solution

To hide a class during the import process, use the renaming syntax shown in Recipe 7.3, “Renaming Members on Import”, but point the class name to the _ wildcard character.

Scala: How to assign the result of a match expression to a variable

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is one of the shortest recipes, Recipe 3.9, “How to assign the result of a match expression to a variable.”

Problem

You want to return a value from a match expression and assign it to a variable, or use a match expression as the body of a method.

Scala: How to match multiple conditions (patterns) with one case statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.8, “How to match multiple conditions with one case statement.”

Problem

You have a situation where several match conditions require that the same business logic be executed, and rather than repeating your business logic for each case, you’d like to use one copy of the business logic for the matching conditions.

How to use a Scala match expression like a switch statement

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.7, “How to use a Scala match expression like a switch statement.”

Back to top

Problem

You have a situation where you want to create something like a simple Java integer-based switch statement, such as matching the days in a week, months in a year, and other situations where an integer maps to a result.

Back to top

Solution

To use a Scala match expression like a Java switch statement, use this approach:

Table of Contents

  1. Problem
  2. Solution
Back to top

How to enter multiline commands (statements) into the Scala REPL

When you want to test a multiline command/statement in the Scala REPL, you can easily run into a problem where the REPL gets confused, or more accurately, it attempts to evaluate your statement too early.

As a simple example of this, imagine that you want to test the Scala "if" statement syntax. You begin typing your if statement normally, but when you hit [Enter] after your second line, you'll see that everything blows up:

A Scala JDBC connection and SQL SELECT example

Scala JDBC FAQ: How can I use the Java JDBC API in my Scala application?

If you want to use a SQL database with your Scala applications, it's good to know you can still use the traditional Java JDBC programming library to access databases. I just ran a simple JDBC connection and SQL SELECT test, and everything seems to work just as it does in Java.

A Java MySQL DELETE example

Summary: A Java MySQL DELETE example, demonstrating how to issue a SQL DELETE command from your Java source code.