FireFry FireFry - 1 year ago 65
Scala Question

How to organize classes inside scala project?

In Java I used to put classes inside packages with long informative names by template

domain.company.project.module.ets
and used to names like:


  • ch.qos.logback.classic.net

  • org.hibernate.cache

  • com.google.common.collect



But in the sources of akka and sbt projects there are a lot of classes inside packages with simple names:


  • akka.actor

  • sbt



And that's not all. There are many classes inside classes. And by classes I also mean objects and traits in all possible combinations. There are object inside traits, traits inside classes, etc. But the level of nesting is always 1 (or should I say 2?).

Obviously, there is another approach to class organization. I wonder if you can give me a tip of how to name packages in scala and when to put classes inside each other?

Answer Source

Hi I will reccomend you to read chater seven of scala for the imatient is free available here: http://typesafe.com/resources/free-books.

As a summary the keypoints, as sayed in the book:

- Packages nest just like inner classes.
- Package paths are not absolute.
- A chain x.y.z in a package clause leaves the intermediate packages x and x.y invisible.
- Package statements without braces at the top of the file extend to the entire file.
- A package object can hold functions and variables.
- Import statements can import packages, classes, and objects.
- Import statements can be anywhere.
- Import statements can rename and hide members.
- java.lang, scala, and Predef are always imported.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download