Sample run of chapter's interpreter examples
// In file packages-and-imports/ex1/Ex1.scala package bobsrockets.navigation class Navigator
package bobsrockets.navigation: class Navigator
package bobsrockets: package navigation: // In package bobsrockets.navigation class Navigator package launch: // In package bobsrockets.navigation.launch class Booster
// In file packages-and-imports/ex4/Ex4.scala package bobsrockets: package navigation: class Navigator: // No need to say bobsrockets.navigation.StarMap val map = new StarMap class StarMap class Ship: // No need to say bobsrockets.navigation.Navigator val nav = new navigation.Navigator package fleets: class Fleet: // No need to say bobsrockets.Ship def addShip = new Ship
package bobsrockets: class Ship package bobsrockets.fleets: class Fleet: // Doesn't compile! Ship is not in scope. def addShip = new Ship
// In file packages-and-imports/launch.scala // In file launch.scala package launch: class Booster3 // In file bobsrockets.scala package bobsrockets: package launch: class Booster2 package navigation: package launch: class Booster1 class MissionControl: val booster1 = new launch.Booster1 val booster2 = new bobsrockets.launch.Booster2 val booster3 = new _root_.launch.Booster3
package bobsrockets package fleets class Fleet: // No need to say bobsrockets.Ship def addShip = new Ship
// In file packages-and-imports/Fruits.scala package bobsdelights abstract class Fruit( val name: String, val color: String ) object Fruits: object Apple extends Fruit("apple", "red") object Orange extends Fruit("orange", "orange") object Pear extends Fruit("pear", "yellowish") val menu = List(Apple, Orange, Pear)
// In file packages-and-imports/Ex5.scala // easy access to Fruit import bobsdelights.Fruit // easy access to all members of bobsdelights import bobsdelights.* // easy access to all members of Fruits import bobsdelights.Fruits.*
// In file packages-and-imports/Ex5.scala def showFruit(fruit: Fruit) = import fruit.* s"{name}s are color"
// In file packages-and-imports/Ex6.scala import java.util.regex class AStarB: // Accesses java.util.regex.Pattern val pat = regex.Pattern.compile("a*b")
// In file packages-and-imports/Ex7.scala import Laptops.* import Fruits.{Apple as _, *}
import java.lang.* // everything in the java.lang package import scala.* // everything in the scala package import Predef.* // everything in the Predef object
// In file packages-and-imports/Ex8.scala.err class Outer: class Inner: private def f = "f" class InnerMost: f // OK (new Inner).f // error: f is not accessible
// In file packages-and-imports/Ex9.scala.err package p: class Super: protected def f = "f" class Sub extends Super: f class Other: (new Super).f // error: f is not accessible
// In file packages-and-imports/ex10/Ex10.scala package bobsrockets package navigation: private[bobsrockets] class Navigator: protected[navigation] def useStarChart() = {} class LegOfJourney: private[Navigator] val distance = 100 package launch: import navigation.* object Vehicle: private[launch] val guide = new Navigator
// In file packages-and-imports/Rocket.scala class Rocket: import Rocket.fuel private def canGoHomeAgain = fuel > 20 object Rocket: private def fuel = 10 def chooseStrategy(rocket: Rocket) = if rocket.canGoHomeAgain then goHome() else pickAStar() def goHome() = {} def pickAStar() = {}
// In file packages-and-imports/package.scala // In file ShowFruit.scala package bobsdelights def showFruit(fruit: Fruit) = import fruit.* s"{name}s are color" // In file PrintMenu.scala package printmenu import bobsdelights.Fruits import bobsdelights.showFruit object PrintMenu: def main(args: Array[String]) = println( for fruit <- Fruits.menu yield showFruit(fruit) )
case class PosInt(value: Int): require(value > 0)
val x = PosInt(88) x.value + 1 // 89
case class PosInt(value: Int): require(value > 0) def +(x: Int): Int = value + x
val x = PosInt(77) x + 1 // 78
case class PosInt(value: Int): require(value > 0) export value.*
val x = PosInt(99) x + 1 // 100 x - 1 // 98 x / 3 // 33
final def +(x: Int): Int = value + x
val x = PosInt(24) x << 1 // 48 (shift left) x >> 1 // 12 (shift right) x >>> 1 // 12 (unsigned shift right)
case class PosInt(value: Int): require(value > 0) export value.{<< as shl, >> as shr, >>> as ushr, *}
val x = PosInt(24) x shl 1 // 48 x shr 1 // 12 x ushr 1 // 12
case class PosInt(value: Int): require(value > 0) export value.{<< as shl, >> as shr, >>> as _, *}
scala> val x = PosInt(39) val x: PosInt = PosInt(39) scala> x shr 1 val res0: Int = 19 scala> x >>> 1 1 |x >>> 1 |^^^^^ |value >>> is not a member of PosInt
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. |