Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 13: Packages and Importsย >ย 13.3 Imports - Pg. 242

C HAPTER 13 ยท Packages and Imports One final trick is important to know. Sometimes, you end up coding in a heavily crowded scope where package names are hiding each other. In List- ing 13.6, the scope of class MissionControl includes three separate pack- ages named launch ! There's one launch in bobsrockets.navigation , one in bobsrockets , and one at the top level. How would you reference each of Booster1 , Booster2 , and Booster3 ? Accessing the first one is easiest. A reference to launch by itself will get you to package bobsrockets.navigation.launch , because that is the launch package defined in the closest enclosing scope. Thus, you can refer to the first booster class as simply launch.Booster1 . Referring to the sec- ond one also is not tricky. You can write bobrockets.launch.Booster2 and be clear about which one you are referencing. That leaves the question of the third booster class, however. How can you access Booster3 , considering that a nested launch package shadows the top-level one? To help in this situation, Scala provides a package named _root_ that is outside any package a user can write. Put another way, every top-level package you can write is treated as a member of package _root_ . For exam- ple, both launch and bobsrockets of Listing 13.6 are members of package _root_ . As a result, _root_.launch gives you the top-level launch pack-