50

While browsing the source code of the Scala API, I met this package definition in scala/tags/R_2_8_0_final/src/library/scala/util/parsing/combinator/syntactical/StdTokenParsers.scala:

package scala.util.parsing
package combinator
package syntactical

What does that mean? That the class will be available in more than one package?

2 Answers 2

67

This is basically the same as

package scala.util.parsing.combinator.syntactical

import scala.util.parsing._
import scala.util.parsing.combinator._

...

So by "stacking" the packages the way you wrote you can get super-packages in scope. See also these answers.

[Update] Here is a new article written by Martin Odersky about this topic: http://www.artima.com/scalazine/articles/chained_package_clauses_in_scala.html

Sign up to request clarification or add additional context in comments.

Comments

7

The Scala reference mentions (chapter 9, section 9.1):

A compilation unit consists of a sequence of packagings, import clauses, and class and object definitions, which may be preceded by a package clause.

A compilation unit

package p1;
...
package pn;
stats

starting with one or more package clauses is equivalent to a compilation unit consisting of the packaging

package p1 { ...
  package pn {
   stats
  } ...
}

See also Organizing Code in Files and Namespaces on nested packages.
(From Programming Scala: Scalability = Functional Programming + Objects By Dean Wampler, Alex Payne, page 44, Chapter 2)

3 Comments

Last link is not available now, please correct or remove it.
@bartektartanus I have restored the link.
still not viewable. Please replace with a simple citation (book, author, publisher, page) perhaps with a link to the book homepage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.