- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
What's new in Scala 2.8 - Package Objects
Created by odersky on 2010-09-14.
Updated: 2010-09-14, 10:15
Until now, the only things you could put in a package were classes, traits, and standalone objects. Scala 2.8 drops that restriction. Any kind of definition that you can put inside a class, you can also put at the top level of a package. To do so, put the definitions in a package object. Find out more here.
- Login or register to post comments
Printer-friendly version
Re: What's new in Scala 2.8 - Package Objects
Might pay to add a section on the current restrictions, notably that methods may not be overloaded.
Re: What's new in Scala 2.8 - Package Objects
Thanks for the comment. I made a note of this now.
Re: What's new in Scala 2.8 - Package Objects
Thanks for the ever-growing scala documentation!
The package object sounds very interesting to me, as it emphasizes the uniform "view" on classes, objects and packages even more (unlike java, which even needs a "static" notion for value imports).
As it seems to me, val's in package objects are now partially able to replace object's within the package without breaking any client code (when they start with an uppercase letter). Would it in theory be possible that one day, the object keyword is completely replaceable by the "val" keyword, since objects are after all only vals defined on package-level?
Re: What's new in Scala 2.8 - Package Objects
There is a difference: A val in a package object has to be defined in the same source file as the package object. An object in a package can be defined in its own source file. But in principle you are right, objects are lazy vals of a certain kind.
Re: What's new in Scala 2.8 - Package Objects
Thanks for the reply.
Are there any more differences than that, except for the simple fact that the scala compiler will treat them a bit differently and has to take care of how java's static keyword translates into the scala world?
I am asking that because i am courius if the new concept of package objects would make the "object" keyword a kind of redundant modifier for "lazy val" for most usecases. A package would then be nothing more than another kind of "code-unit" which could, unlike normal classes and definitions, split its content over multiple source files whose content is seen as a whole at runtime ( and partially "merged" by the scala compiler).
Re: What's new in Scala 2.8 - Package Objects
There is a small mistype. "gardening.fruit.package", must be "gardening.fruits.package".