switch

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

Using Scala match expressions with XML

Problem: You want to use match expressions as another way to access the information contained in XML data when writing your Scala applications.

Solution

Given this XML literal:

Java enum examples/tutorial (an enum reference page)

Java enum FAQ: Can you share some Java enum examples, such as how to declare a Java enum, and how to use a Java enum in a for loop, if then statement, and Java switch statement?

As described in Sun's Java documentation, a Java enum "is a type whose fields consist of a fixed set of constants ... you should use enum types any time you need to represent a fixed set of constants." Let's take a look at some Java enum examples to see how this works.

A Java enum switch/case statement example

Java enum FAQ: Can you share a Java enum switch example, i.e., how to use an enum with a Java switch statement?

In my earlier Java enum examples tutorial, I demonstrated how to declare a simple Java enum, and then how to use a Java enum with a variety of Java constructs, including a Java switch statement, a for loop, and an if/then statement.

Apple makes me use a Microsoft keyboard

My only rant against Apple these days is that when I use my iMac I've had to resort to using a Microsoft keyboard. That's because at the ripe age of 45 I have some arthritis in my fingers, and the keys on Apple's keyboards are too hard to press. You might be surprised, but it really hurts to press those hard keys thousands of times a day.

How to get help on Windows/DOS commands

Okay, this is one small step for me, and not really any steps for mankind ... I never know how to get help on Windows/DOS commands. I try command line options/switches like -h, -help, --help, /help, and \help, and of course none of them works.

Then somehow today I learned that the magic incantation, er, command line option, is /?, like this:

taskkill /?

Duh.

 

Java switch statement example

Java switch case statement FAQ: Can you provide an example of a Java switch/case statement syntax?

Here's a sample Java method that takes an int argument and attempts to turn that int into a month string, using a Java switch case statement to make that decision:

On switching back from Rails to PHP

In the latest release of Dr. Dobbs, Michael Swaine reports that "after two years trying to make Ruby on Rails do what he wanted, Derek Sivers went back to PHP and finished the job in two months." Actually, here's a direct link to Derek Sivers' post, including his seven reasons he switched back from Rails to PHP, and what he learned from Rails.

 

Mon, Feb 27, 2006 (Making the switch)

Some time in the very near future I will make the switch. Yesterday I placed an order for a MacBook Pro, and now I anxiously await its arrival.

Before discussing my reasons for making the switch, let me first state that I am not currently an iPod or iTunes user. I may get to those things eventually, but for now those things are non factors, and what I'm most interested in is developing new software for customers, and designing new software systems.