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 7779741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:42:55+00:00 2026-06-01T18:42:55+00:00

When I try to build internal DSLs in Scala, I run into a common

  • 0

When I try to build internal DSLs in Scala, I run into a common problem and I haven’t been able to craft a solution. To make things look a bit more like a typical language, I’d like the syntax to look something like this:

model 'Foo {
  decl 'Real 'x;
  decl 'Real 'y;
}

In practice, there are several issues. The first issue is getting a model object here to take two arguments in this way. If anybody has any ideas, let me know. But what I’ve done instead is to do something a bit more like this:

model('Foo) {
  ...
}

Where model is now a function which then returns an object with an apply method which then consumes the lambda that follows. That I can live with. I could live with a similar issue inside the lambda as well, so things like decl 'Real 'x or decl('Real,'x) on the inside. But what I want to do is to get the results of all those expressions inside the squiggly braces to get “returned” as a list. In other words, what I want is to write something like this:

model 'Foo {
  decl('Real,'x);
  decl('Real,'y);
}

where decl(...) evaluates to something of type Declaration and the {...} then evaluates to List[Declaration]. I suspect there is some way of using implicits to do this, but I haven’t been able to find it. In short, I’d like to make:

model 'Foo {
  decl('Real,'x);
  decl('Real,'y);
}

…evaluate to the equivalent of…

model 'Foo {
  decl('Real,'x) ::
  decl('Real,'y) ::
  Nil
}

Comments or suggestions?

  • 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-01T18:42:56+00:00Added an answer on June 1, 2026 at 6:42 pm

    As a first idea, you could try variable arguments lists, which allows you to use commas instead of semi-colons:

    case class Declaration(name: String)
    
    def decl( s: String ) = Declaration(s)
    
    case class Model( sym: Symbol, decls: List[Declaration] )
    
    def model( sym: Symbol)( decls: Declaration* ) =
      Model( sym, decls.toList )
    
    val m = model( 'Foo )(
      decl( "bar" ), 
      decl( "baz" ) 
    )
    

    Alternatively, you could extend a trait to get rid of some parentheses and of the commas:

    case class ModelBuilder( sym: Symbol ) {
      def using( decls: Declarations ) = Model( sym, decls.toList )
    }
    
    trait Declarations {
    
      protected var decls = List[Declaration]()
    
      protected def decl( s: String ) = 
    decls ::= Declaration( s )
    
      def toList = decls
    }
    
    def model( sym: Symbol ) = ModelBuilder( sym )
    
    model( 'Foo ) using new Declarations {
      decl( "bar" )
      decl( "baz" )
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I have a try to build and run sample hello world program of
I try build wrap panel with vertical buttons. Every button consist from image and
I try to build a gui (Swing) for a simple java application. The application
i try to build a app that is in landsscape mode all the time.
Im try to build my plugins that sit in a seperate directory on the
I try to build libtorrent(rasterbar- libtorrent). It cannot be build. the build log is
I try to build project in Eclipse on Linux Ubuntu. Eclipse show error message:
When I try to build my project I get the following message in the
When I try to build my projects in Visual Studio 2008, web sites won't
When I try to build a project in Visual Studio 2005 that has a

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.