Reference-able Package Objects
One limitation with package object
s 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.b
only expands toa.b.package
when used "standalone", i.e. not when part of a larger select chaina.b.c
or equivalent postfix expressiona.b c
, prefix expression!a.b
, or infix expressiona.b c d
. -
a.b
expands toa.b.package
of the typea.b.package.type
, and only contains the contents of thepackage object
. It does not contain other things in thepackage
a.b
that 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 object
s and normal object
s.