At least one case where this template comes in handy is when you create a library of functions. You can rearrange functions (actually methods, but let them use them in this context) in a few ways (which can then be considered modules), say, MyLibA , MyLibB , MyLibC , etc. Then, if for each of them you define an object that implements it, users of your library can easily use it by writing, for example:
import mypackage.MyLibA._
(assuming mypackage is a containing package). In addition, you can easily provide a way to import all the functions of your lib by providing a MyLib object as follows:
object MyLib extends MyLibA with MyLibB with MyLibC
and then users can simply import mypackage.MyLib._ instead of writing an import for each module separately. Even better, you can define package object mypackage extends MyLibA with MyLibB with MyLibC , and then users just write import mypackage._ . And the bonus is that discerning users who would like to import only MyLibA and MyLibB on one line can also freely define their own “import object”, perhaps even filling it with their own utility functions or overriding their own
object UserLibImport extends MyLibA with MyLibB { def userAdditionalFunction = override def someRedefinedLibAFunction = }
... and then import it all with import UserLibImport._
So, I would say, in this case, this is not only a good style, it is strongly recommended to provide your functions in this way, since it provides maximum flexibility.
(This has also been briefly described in this answer .)
Jean-philippe pellet
source share