Sample run of chapter's interpreter examples
class ChecksumAccumulator: // class definition goes here, indented
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) = 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
// In file classes-and-objects/SemiInference.scala val s = "hello"; println(s)
// In file classes-and-objects/SemiInference.scala if x < 2 then "too small" else "ok"
// 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) then cache(s) else val acc = new ChecksumAccumulator for c <- s do acc.add((c >> 8).toByte) acc.add(c.toByte) val cs = acc.checksum() cache += (s -> cs) cs
ChecksumAccumulator.calculate("Every value is an object.")
case class Person(name: String, age: Int)
val p = Person("Sally", 39)
p.name // Sally p.age // 39
p.toString // Person(Sally,39)
p == Person("Sally", 21) // false p.hashCode == Person("Sally", 21).hashCode // false p == Person("James", 39) // false p.hashCode == Person("James", 39).hashCode // false p == Person("Sally", 39) // true p.hashCode == Person("Sally", 39).hashCode // true
case class Person(name: String, age: Int): def appendToName(suffix: String): Person = Person(s"namesuffix", age) object Person: // Ensure non-empty name is capitalized def apply(name: String, age: Int): Person = val capitalizedName = if !name.isEmpty then val firstChar = name.charAt(0).toUpper val restOfName = name.substring(1) s"firstCharrestOfName" else throw new IllegalArgumentException("Empty name") new Person(capitalizedName, age)
val q = Person("sally", 39) // Person(Sally,39)
q.appendToName(" Smith") // Person(Sally Smith,39)
// In file classes-and-objects/Summer.scala // In file Summer.scala import ChecksumAccumulator.calculate object Summer: def main(args: Array[String]): Unit = for arg <- args do println(arg + ": " + calculate(arg))
$ scalac ChecksumAccumulator.scala Summer.scala
$ scala Summer of love
of: -213 love: -182
For more information about Programming in Scala, Fifth Edition (the "Stairway Book"), please visit: http://www.artima.com/shop/programming_in_scala_5ed and: |
Copyright © 2007-2020 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. |