Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the _root_ package in Scala?

I'm using IntelliJ IDEA with the Scala plugin. If I reference HashMap in code, and then use Alt-Enter to add the import, the package gets imported as:

_root_.scala.collection.immutable.HashMap 

What's the root part of this? It seems to work with and without it.

like image 304
Landon Kuhn Avatar asked Mar 26 '09 18:03

Landon Kuhn


People also ask

What are different packages in Scala?

Package in Scala is a mechanism to encapsulate a group of classes, sub packages, traits and package objects. It basically provides namespace to put our code in a different files and directories. Packages is a easy way to maintain our code which prevents naming conflicts of members of different packages.

What is Scala package object?

Scala provides package objects as a convenient container shared across an entire package. Package objects can contain arbitrary definitions, not just variable and method definitions. For instance, they are frequently used to hold package-wide type aliases and implicit conversions.


2 Answers

It has to do scala imports being relative - _root_ gives you a way to specify an absolute package name. See the Scala Wiki

like image 51
Kevin Hakanson Avatar answered Oct 12 '22 02:10

Kevin Hakanson


The Scala language specification has this to say about _root_ in section 9.4 Package References

The special predefined name _root_ refers to the outermost root package which contains all top-level packages.

See the following PDF for the full language reference: http://www.scala-lang.org/docu/files/ScalaReference.pdf

like image 26
Alan LaMielle Avatar answered Oct 12 '22 01:10

Alan LaMielle