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.
Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but traits may not have constructor parameters. A trait definition looks just like a class definition except that it uses the keyword trait.
Traits are similar in spirit to interfaces in Java programming language. Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters. However, you can inherit (extend) them using classes and objects.
Classes in Scala are blueprints for creating objects. They can contain methods, values, variables, types, objects, traits, and classes which are collectively called members.
In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.
Before 2.8, you had to choose between packages and objects. The problem with packages is that they cannot contain methods or vals on their own. So you have to put all those inside another object, which can get awkward. Observe:
object Encrypt { private val magicConstant = 0x12345678 def encryptInt(i: Int) = i ^ magicConstant class EncryptIterator(ii: Iterator[Int]) extends Iterator[Int] { def hasNext = ii.hasNext def next = encryptInt(ii.next) } }
Now you can import Encrypt._
and gain access to the method encryptInt
as well as the class EncryptIterator
. Handy!
In contrast,
package encrypt { object Encrypt { private[encrypt] val magicConstant = 0x12345678 def encryptInt(i: Int) = i ^ magicConstant } class EncryptIterator(ii: Iterator[Int]) extends Iterator[Int] { def hasNext = ii.hasNext def next = Encrypt.encryptInt(ii.next) } }
It's not a huge difference, but it makes the user import both encrypt._
and encrypt.Encrypt._
or have to keep writing Encrypt.encryptInt
over and over. Why not just use an object instead, as in the first pattern? (There's really no performance penalty, since nested classes aren't actually Java inner classes under the hood; they're just regular classes as far as the JVM knows, but with fancy names that tell you that they're nested.)
In 2.8, you can have your cake and eat it too: call the thing a package object, and the compiler will rewrite the code for you so it actually looks like the second example under the hood (except the object Encrypt
is actually called package
internally), but behaves like the first example in terms of namespace--the vals and defs are right there without needing an extra import.
Thus, projects that were started pre-2.8 often use objects to enclose lots of stuff as if they were a package. Post-2.8, one of the main motivations has been removed. (But just to be clear, using an object still doesn't hurt; it's more that it's conceptually misleading than that it has a negative impact on performance or whatnot.)
(P.S. Please, please don't try to actually encrypt anything that way except as an example or a joke!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With