Code Examples for

Programming in Scala, Fourth Edition

Return to chapter index

13 Packages and Imports

  • 13.1 Putting code in packages
  • 13.2 Concise access to related code
  • 13.3 Imports
  • 13.4 Implicit imports
  • 13.5 Access modifiers
  • 13.6 Package objects
  • 13.7 Conclusion
  • 13.1 Putting code in packages


    // 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 tests { // In package bobsrockets.navigation.tests class NavigatorSuite } } }
    // 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 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 launch { class Booster2 } }

    13.2 Concise access to related code


    package bobsrockets package fleets class Fleet { // No need to say bobsrockets.Ship def addShip() = { new Ship } }

    13.3 Imports


    // 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._ println(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 Notebooks._ import Fruits.{Apple => _, _}

    13.4 Implicit imports


    import java.lang._ // everything in the java.lang package import scala._ // everything in the scala package import Predef._ // everything in the Predef object

    13.5 Access modifiers


    // In file packages-and-imports/Ex8.scala.err class Outer { class Inner { private def f() = { println("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() = { println("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 } private[this] var speed = 200 } } package launch { import navigation._ object Vehicle { private[launch] val guide = new Navigator } }
    val other = new Navigator other.speed // this line would not compile
    // 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) goHome() else pickAStar() } def goHome() = {} def pickAStar() = {} }

    13.6 Package objects


    // In file packages-and-imports/package.scala // In file bobsdelights/package.scala package object bobsdelights { def showFruit(fruit: Fruit) = { import fruit._ println(name + "s are " + color) } } // In file PrintMenu.scala package printmenu import bobsdelights.Fruits import bobsdelights.showFruit object PrintMenu { def main(args: Array[String]) = { for (fruit <- Fruits.menu) { showFruit(fruit) } } }

    13.7 Conclusion

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

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

    and:

    http://booksites.artima.com/programming_in_scala_4ed

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