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

  • Home
  • SEARCH
  • 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 9215623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:14:42+00:00 2026-06-18T02:14:42+00:00

Playing about with a DSL in Scala, so lets say I have something like

  • 0

Playing about with a DSL in Scala, so lets say I have something like this:

house {
  floor {
    bedroom("kids)
    bedroom("master")
  }
  floor {
    kitchen()
  }
}

Now what I want is at each nested block it to have a reference or be referencing functions on the enclosing block. Eg so the effect is that floor is added to the house, bedroom to the floor etc.

Currently I do this in a horrible fashion of having a global stack that gets updated at each nested level to keep track of the current “context”. Also my current version is not typesafe in that I can add a bedroom to a house.

Another previous revision was

house {
  floor {
    bedroom("kids) +
      bedroom("master")
  } +
    floor {
      kitchen()
    }
}

Where each block returned a List of widgets (the + was using an implicit to turn a generic “thing” into a “thing list” so that the next “thing” can be added). The returned list of widgets was then added once the block returned. But I do not like the forced use of + as it gets ugly on many pages worth.

Anyway to meld the two ?

  • 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-06-18T02:14:44+00:00Added an answer on June 18, 2026 at 2:14 am

    Do you really need each block to have a reference to the enclosing block? Or was it just so that you could add the nested block to the parent block? In this case you could simply pass nested blocks to the enclosing block, so to speak:

    house (
      floor (
        bedroom("kids"),
        bedroom("master")
      ),
      floor (
        kitchen
      )
    )
    

    Using the following definitions:

    trait HouseElement
    case class house( elements: HouseElement* )
    trait FloorElement
    case class floor( elements: FloorElement * ) extends HouseElement
    case class bedroom( name: String ) extends FloorElement
    case object kitchen extends FloorElement
    

    Otherwise, another solution is to rely heavily on anonymous classes (which unfortunately requires to use the new keyword everywhere):

    new house {
      new floor {
        new bedroom("kids")
        new bedroom("master")
      }
      new floor {
        new kitchen()
      }
    }
    

    Using the following definitions:

    import collection.mutable.Buffer
    class house {
      val elements = Buffer[Element]()
      trait Element {
        elements += this 
      }        
      class floor extends Element { 
        val elements = Buffer[Element]()
        trait Element {
          elements += this 
        }        
        class bedroom(name: String) extends Element 
        class kitchen extends Element
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been playing about with the layout of a view with Android (lets say
I am playing about with the Zend Framework at the moment and I have
I've been playing about with Runnable s and have discovered that if you postDelayed
I have been playing about with canvas, but have stumbled across a problem. When
I've been playing about with this plugin over the last few days, customising it
I'm playing about with a stop words within my code I have an array
I've been playing about with CSS3 columns and have come across a positioning issue.
I have been playing about with iText to try get a list of embedded
I have been playing about with LINQ-SQL, trying to get re-usable chunks of expressions
I have been playing with solr for about a week now. My issue is

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.