Reference-able Package Objects
One limitation with package objects is that we cannot currently assign them to values: a.b fails to compile when b is a package object, even though it succeeds when b is a normal object. The workaround is to call
a.b.`package`
But this is ugly and non-obvious. Or one could use a normal object, which is not always possible.
The packageObjectValues language extension drops this limitation. The extension is enabled by the language import import scala.language.experimental.packageObjectValues or by setting the command line option -language:experimental.packageObjectValues.
The extension, turns the following into valid code:
package a
package object b
val z = a.b // Currently fails with "package is not a value"
Currently the workaround is to use a .package suffix:
val z = a.b.`package`
With the extension, a reference such as a.b where b is a package containing a package object, expands to a.b.package automatically
Limitations
-
a.bonly expands toa.b.packagewhen used "standalone", i.e. not when part of a larger select chaina.b.cor equivalent postfix expressiona.b c, prefix expression!a.b, or infix expressiona.b c d. -
a.bexpands toa.b.packageof the typea.b.package.type, and only contains the contents of thepackage object. It does not contain other things in thepackagea.bthat are outside of thepackage object
Both these requirements are necessary for backwards compatibility, and anyway do not impact the main goal of removing the irregularity between package objects and normal objects.