Reference-able Package Objects
This feature is available under -preview since Scala 3.10.0. It is specified in SIP-68.
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.
This preview feature drops that limitation. It is enabled by the -preview compiler flag.
The feature turns the following into valid code:
package a
package object b
val z = a.b
Without the feature, the workaround is to use a .package suffix:
val z = a.b.`package`
With the feature, 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.