Code Examples for

Programming in Scala, Second Edition

Return to chapter index

4 Classes and Objects

Sample run of chapter's interpreter examples

4.1 Classes, fields, and methods


class ChecksumAccumulator { // class definition goes here }
new ChecksumAccumulator
// In file classes-and-objects/ChecksumCalculator1.scala class ChecksumAccumulator { var sum = 0 }
val acc = new ChecksumAccumulator val csa = new ChecksumAccumulator
acc.sum = 3
// Won't compile, because acc is a val acc = new ChecksumAccumulator
class ChecksumAccumulator { private var sum = 0 }
val acc = new ChecksumAccumulator acc.sum = 5 // Won't compile, because sum is private
// In file classes-and-objects/ChecksumCalculator2.scala class ChecksumAccumulator { private var sum = 0 def add(b: Byte): Unit = { sum += b } def checksum(): Int = { return ~(sum & 0xFF) + 1 } }
def add(b: Byte): Unit = { b = 1 // This won't compile, because b is a val sum += b }
// In file classes-and-objects/ChecksumCalculator3.scala class ChecksumAccumulator { private var sum = 0 def add(b: Byte): Unit = sum += b def checksum(): Int = ~(sum & 0xFF) + 1 }
// In file classes-and-objects/ChecksumCalculator4.scala // In file ChecksumAccumulator.scala class ChecksumAccumulator { private var sum = 0 def add(b: Byte) { sum += b } def checksum(): Int = ~(sum & 0xFF) + 1 }
scala> def f(): Unit = "this String gets lost" f: ()Unit
scala> def g() { "this String gets lost too" } g: ()Unit
scala> def h() = { "this String gets returned!" } h: ()java.lang.String scala> h res0: java.lang.String = this String gets returned!

4.2 Semicolon inference


// In file classes-and-objects/SemiInference.scala val s = "hello"; println(s)
// In file classes-and-objects/SemiInference.scala if (x < 2) println("too small") else println("ok")
x + y
(x + y)
x + y + z

4.3 Singleton objects


// In file classes-and-objects/ChecksumAccumulator.scala // In file ChecksumAccumulator.scala import scala.collection.mutable.Map object ChecksumAccumulator { private val cache = Map[String, Int]() def calculate(s: String): Int = if (cache.contains(s)) cache(s) else { val acc = new ChecksumAccumulator for (c <- s) acc.add(c.toByte) val cs = acc.checksum() cache += (s -> cs) cs } }
ChecksumAccumulator.calculate("Every value is an object.")

4.4 A Scala application


// In file classes-and-objects/Summer.scala // In file Summer.scala import ChecksumAccumulator.calculate object Summer { def main(args: Array[String]) { for (arg <- args) println(arg +": "+ calculate(arg)) } }
$ scalac ChecksumAccumulator.scala Summer.scala
$ fsc ChecksumAccumulator.scala Summer.scala
$ scala Summer of love
of: -213 love: -182

4.5 The Application trait


// In file classes-and-objects/FallWinterSpringSummer.scala import ChecksumAccumulator.calculate object FallWinterSpringSummer extends Application { for (season <- List("fall", "winter", "spring")) println(season +": "+ calculate(season)) }

4.6 Conclusion


object EchoArgs { def main(args: Array[String]) { for (arg <- args) print(arg + " ") println() }

For more information about Programming in Scala, Second Edition (the "Stairway Book"), please visit:

http://www.artima.com/shop/programming_in_scala_2ed

and:

http://booksites.artima.com/programming_in_scala_2ed

Copyright © 2007-2010 Artima, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.