Code Examples for

Programming in Scala, Third 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/ChecksumAccumulator.scala class ChecksumAccumulator { private var sum = 0 def add(b: Byte) = sum += b def checksum() = ~(sum & 0xFF) + 1 }
// In file ChecksumAccumulator.scala class ChecksumAccumulator { private var sum = 0 def add(b: Byte): Unit = { sum += b } def checksum(): Int = ~(sum & 0xFF) + 1 }

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 object ChecksumAccumulator { private val cache = mutable.Map.empty[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 App trait


// In file classes-and-objects/FallWinterSpringSummer.scala import ChecksumAccumulator.calculate object FallWinterSpringSummer extends App { 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, Third Edition (the "Stairway Book"), please visit:

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

and:

http://booksites.artima.com/programming_in_scala_3ed

Copyright © 2007-2016 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.