Channels ▼
RSS

JVM Languages

Scala for C# Developers: Useful Features


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:

  def move(dx: Double, dy: Double, dz: Double) : Point3D = {
      return new Point3D(x + dx, y + dy, z + dz);
  }

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

The Scala console displaying the results of evaluating the different expressions with the instances of Point3D
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.

scala> immutablePoint3D.x
res0: Double = 10.0

scala> immutablePoint3D.y
res1: Double = 20.0

scala> immutablePoint3D.z
res2: Double = 5.0

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:

scala> val movedPoint3D = immutablePoint3D.move(5, 5, 5)
movedPoint3D: Point3D = Point3D@70111d6d

scala> movedPoint3D.x
res3: Double = 15.0

scala> movedPoint3D.y
res4: Double = 25.0
  
scala> movedPoint3D.z
res5: Double = 10.0

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.

class Point3D(val x: Double, val y: Double, val z: Double) {
	def move(dx: Double, dy: Double, dz: Double) : Point3D = new Point3D(x + dx, y + dy, z + dz)
	def +(dx: Double, dy: Double, dz: Double) : Point3D = move(dx, dy, dz)
	def -(dx: Double, dy: Double, dz: Double) : Point3D = move(-dx, -dy, -dz)
	def dx(delta: Double) : Point3D = move(delta, 0, 0)
	def dy(delta: Double) : Point3D = move(0, delta, 0)
	def dz(delta: Double) : Point3D = move(0, 0, delta)
}

The new version of the Point3D class adds the following methods to the previous version. Each method returns a new Point3D instance containing the new values for the x, y and z fields:

  • +: Increases the values of x, y, and z with the received parameters.
  • -: Reduces the values of x, y, and z with the received parameters.
  • dx: Increases the value of x with the received parameter.
  • dy: Increases the value of y with the received parameter.
  • dz: Increases the value of z with the received parameter.

Now, enter the following expression in the Scala Console to create a new immutable reference to an instance of the new version of the Point3D class:

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

Because the new Point3D class defined a method named +, you can call it with the following line and the code will have the same effect as calling the move method:

  val movedPoint3D = immutablePoint3D.+(5, 5, 5)

The good news is that you can call the method named + without using the dot (.), as shown in the next lines:

  val movedPoint3D = immutablePoint3D + (5, 5, 5)
  (movedPoint3D.x, movedPoint3D.y, movedPoint3D.z)

This way, the method named + works as the + operator does in other languages. The same mechanism applies to the method named -. The following lines call the method named - without using the dot (.):

  val movedPoint3D = immutablePoint3D - (3, 3, 3)
  (movedPoint3D.x, movedPoint3D.y, movedPoint3D.z)

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:

scala> val myPoint = new Point3D(10, 10, 10)
myPoint: Point3D = Point3D@2e7c107b

scala> val myPoint2 = myPoint dx 1 dy 2 dz 3
myPoint2: Point3D = Point3D@1e1d8d1

scala> (myPoint2.x, myPoint2.y, myPoint2.z)
res5: (Double, Double, Double) = (11.0,12.0,13.0)

The following two lines produce exactly the same result in Scala. The second line is similar to the code you'd use in C#:

  val myPoint2 = myPoint dx 1 dy 2 dz 3
  val myPoint2 = myPoint.dx(1).dy(2).dz(3)

Conclusion

In the next article, I'll dive deep on the way you can mix in many traits. I'll also explain some important differences between Scala and C#, such as pattern matching and if expressions.

Related Article

Scala for C# Developers: A Tutorial

Related Video

Martin Odersky on Scala

Gaston Hillar is a frequent contributor to Dr. Dobb's.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video