Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3752656
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:16:30+00:00 2026-05-19T09:16:30+00:00

Ok, I’ll explain why I ask this question. I begin to read Lift 2.2

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:16:31+00:00Added an answer on May 19, 2026 at 9:16 am

    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!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.