A “Luke/Vader” light switch. Clever. Geeky.
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
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.
To use a Scala
match expression like a Java
switch statement, use this approach:
Problem: You want to use match expressions as another way to access the information contained in XML data when writing your Scala applications.
Given this XML literal:
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.
Java enum FAQ: Can you share a Java enum switch example, i.e., how to use an enum with a Java
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.
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.
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
\help, and of course none of them works.
Then somehow today I learned that the magic incantation, er, command line option, is
/?, like this:
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:
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.
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.