`package` vs `package object`

98 views
Skip to first unread message

Nikita Volkov

unread,
Jun 4, 2012, 6:56:56 AM6/4/12
to scala-...@googlegroups.com
What practical difference does it make from a user's perspective to declare a member in the package object or on the package level? 

The only difference I know about is that `package` does not allow declaration of functions and `package object` allows anything, but warns you not to declare classes in it. But why should the user be even bothered by such a distinction, and moreover what does a newcomer have to learn of such a concept as `package object` for, when it can be easily eliminated by simply enabling all its functionality on the package level?

The way I see it, `package object` is a supposed to be internal compiler-level abstraction that somehow crawled out into the actual language API. Therefore I want to open a debate on deprecating it and allowing declaration of any member in the `package { }` block or on the package level.

Chris Hodapp

unread,
Jun 4, 2012, 7:12:04 AM6/4/12
to scala-...@googlegroups.com
One problem is that the contents of a package can be distributed across a large number of files (some may even be compiled at a later time than others), but the contents of the package object have to be defined in a single object, ensuring that there is only one place for the compiler to look for package-scoped methods and values. This makes implementing this "let you define methods and values in the package" feature a lot harder to implement than just having the compiler automatically group methods and values defined in the package scope into a hidden object.

Som Snytt

unread,
Jun 4, 2012, 11:43:25 AM6/4/12
to scala-...@googlegroups.com
Also "things to come" may introduce semantics:
http://www.scala-lang.org/docu/files/packageobjects/packageobjects_3.html

HamsterofDeath

unread,
Jun 4, 2012, 12:02:37 PM6/4/12
to scala-...@googlegroups.com
because stuff in a package object is automatically imported in every
class of that package. put some implicits in your root package object
and you'll effectively pimp scala for all your classes
Reply all
Reply to author
Forward
0 new messages