JVM Languages

Scala for C# Developers: Useful Features

Scala's immutable values and mutable variables, classes and constructors, and its use of operators as method names.

When you want to change the values for x, y, or z, you have to call the move method; and you will have a new Point3D instance. Notice that the move method doesn't use a return statement to indicate that the result of the new Point3D(x + dx, y + dy, z + dz) expression must be returned. In Scala, the last expression executed in a method is always a return statement; so you don't have to include the return statement yourself. Another way of writing the move method with code similar to what you're used to seeing in C# is:

To create an instance of the Point3D class, enter the following into the Scala Console:

val immutablePoint3D = new Point3D(10, 20, 5)

The Scala Console will display a line similar to the following one with the result of evaluating the expression: a new instance of the Point3D class named immutablePoint3D, and a unique internal identifier that will be different in your Scala console (Point3D@4bfba7f):

immutablePoint3D: Point3D = Point3D@4bfba7f

You can access the three public fields to check the values for x, y and z. Just enter the following expressions after the scala> prompt and the Scala console will provide the results (Figure 4).

Figure 4: The Scala Console displaying the results of evaluating the different expressions with the instances of Point3D.

Notice that each time you enter an expression and you don't assign the result to a variable, the Scala Console creates a new variable that you will be able to access later. In this case, res0, res1, and res2 are variables of type Double containing the values of the three expressions.

Because the three fields were defined with the val keyword, they are immutable. Now, enter the following expressions after the scala> prompt to call the move method and check the values for x, y, and z for the new Point3D instance returned by the method. The Scala console will provide the results:

Operators as Methods

In Scala, operators are methods like any other methods that you can define for either a class or a trait. The trick is that Scala allows you to use symbols such as +, *, and & as method names. The following lines define a new version of the Point3D class. After you enter them, the Console will display "defined class Point3D," indicating that the class is ready to be used in new expressions.

The following lines create a new instance of Point3D named myPoint and then call three methods: dx, dy, and dz. In this case, the code doesn't use parentheses to enclose the single parameter required for each method. It's easy to read and understand code that increases x 1 point, then, increases y 2 points, and finally increases z 3 points:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!