Alex Rivera | Logout

Why people define class, trait, object inside another object in Scala?

Asked 2011-01-13T14:47:15.543
26

Ok, I'll explain why I ask this question. I begin to read Lift 2.2 source code these days. It's good if you happened to read lift source code before.

In Lift, I found that, define inner class and inner trait are very heavily used.

object Menu has 2 inner traits and 4 inner classes. object Loc has 18 inner classes, 5 inner traits, 7 inner objects.

There're tons of codes write like this. I wanna to know why the author write like this.

  • Is it because it's the author's personal taste or a powerful use of language feature?
  • Is there any trade-off for this kind of usage?
Edit
Report

2 Answers

4

Putting classes, traits and objects in an object is sometimes required when you want to use abstract type variables, see e.g. http://programming-scala.labs.oreilly.com/ch12.html#_parameterized_types_vs_abstract_types

answered 2011-01-13T15:48:12.230
2

sblundy has a decent answer. One thing to add is that only with Scala 2.8 do you have package objects which let you group similar things in a package namespace without making a completely separate object. For that reason I will be updating my Lift Modules proposal to use a package object instead of a simple object.

answered 2011-01-13T15:20:56.140

Your Answer