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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:28:58+00:00 2026-06-15T22:28:58+00:00

I don’t know this field so much. Can someone explain what is possible in

  • 0

I don’t know this field so much.

Can someone explain what is possible in Scala 2.10 with macros, compared to what is possible in Java with compilation preprocessors, and tools like CGLIB, ASM, Byteman…?

  • 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-15T22:28:59+00:00Added an answer on June 15, 2026 at 10:28 pm

    [Update]: I’ve Tried to incorporate an example using Slick. It’s difficult to sum up a lot of this stuff for a Java (non-Scala) audience.

    Macros in Scala 2.10 bring full-blown meta-programming to the language proper, as a first-class citizen.

    // we often do this:
    log("(myList++otherList).size: " + (myList++otherList).size)
    // just to log the string:  
    // "(myList++otherList).size: 42"
    
    // Imagine log, if implemented as a macro:
    log((myList++otherList).size)
    // could potentially log both the EXPRESSION AND IT'S VALUE:
    // "(myList++otherList).size: 42"
    

    Could features like this generally be achieved via either textual preprocessing or
    byte-code manipulation in a safe and clean way?

    Meta-programming comes down to code-generation at some stage, and unqualified, it is an umbrella term for a variety of techniques – in the interest of ‘not having to write a bit of code yourself’ – if one were to enumerate some of these techniques in some rough order of stage – from pre-compiled raw source to executing code, the list might look like:

    • textual source preprocessors (C)
    • template systems (C++) (many might argue that the above are too
      primitive to be considered)
    • reflection ‘naturally available’ in some dynamic languages (Ruby)
    • run-time reflection in statically typed languages (Java), which
      renders some dynamism to the language at the cost of type safety
    • byte code manipulation

    (Notice I’ve omitted macro systems that run at compile time, more about them in the next paragraph.)

    First, consider that all the above techniques essentially generate code – be it the generation/manipulation of plain textual source code or the generation/manipulation of byte code at run time.

    What about macro systems? Macros that run at compile time and operate not on textual source code nor on compiled byte code, but at a far more valuable stage – they work on the AST of the program during compilation and the information available there and the integration with the compilation process renders them some powerful advantages. They are available both in dynamic languages (such as Lisp and Dylan) and in statically typed languages (Template Haskell and Scala 2.10’s self-cleaning Macros).

    Regarding Macros in Scala 2.10, off the top of my head,
    I’d say the most important advantage would be:

    Type Safety: Compilation preprocessors and byte-code manipulation cannot
    take advantage of the type system. With macros – particularly compile-time macros -the kind that Scala 2.10 will have, the macro language is Scala itself with access to the compiler’s API. Any kind of static analysis / checking of source code with full type information that is usually possibly only at compile time will be available to macros.

    (Safe) Syntax Extension: Macros make it possible to adapt language constructs to better implement DSLs. A good example is Slick, a database library that lets you express SQL queries as type-safe Scala code:

    Consider first, plain Scala list processing – no talk of databases or macros yet:

    val coffees : List[Coffee] = // gotten from somewhere
    
    // get from this list a list of (name, price) pairs also filtering on some condition
    for {
      c <- coffees if c.supID == 101
      //                      ^ comparing an Int to an Int - normal stuff.
    } yield (c.name, c.price)
    
    // For Java programmers, the above is Scala's syntactic sugar for
    coffees.filter(c => c.supId == 101).map(c => (c.name, c.price))
    

    Slick, even the non-macro version, lets you treat database tables as if they were
    Scala collections: This is how the non-macro version (Slick calls it the lifted embedding API) achieves the same, were Coffees an SQL table instead:

    // WITHOUT MACROS (using enough of Scala's other features):
    // Generates a query "SELECT NAME,PRICE FROM COFFEES IF SUP_ID = 101" 
    for {
      c <- Coffees if c.supID === 101
      //                      ^ comparing Rep[Int] to Rep[Int]!
      // The above is Scala-shorthand for
      //     c <- Coffees if c.supID.===(Const[Int](101))
    } yield (c.name, c.price)
    

    Close enough! But here the === method is used to imitate == which cannot be used for the obvious reason that you can’t compare the representation of an SQL column with an actual Int.

    This is solved in the macro version, if you have Scala 2.10 handy:

    // WITH MACROS:
    // Generates a query "SELECT NAME,PRICE FROM COFFEES IF SUP_ID = 101" 
    for {
      c <- coffees if c.supID == 101
      //                      ^ comparing Int to Int!
    } yield (c.name, c.price)
    

    So here macros are leveraged to provide the same syntax both for SQL as well as a plain Scala collection. It is the combination of type safety and macro hygiene in addition to the already present expressiveness and compositionality available in Scala that makes Macros attractive.

    Also, consider this example from the link contributed by the other answer:

    def assert(cond: Boolean, msg: Any) = macro impl
    
    def impl(c: Context)(cond: c.Expr[Boolean], msg: c.Expr[Any]) =
      if (assertionsEnabled)
        // if (!cond) raise(msg)
        If(Select(cond.tree, newTermName("$unary_bang")),
            Apply(Ident(newTermName("raise")), List(msg.tree)),
            Literal(Constant(())))
      else
        Literal(Constant(())
    

    So that defines a macro assert, whose usage will resemble a method call:

    import assert
    assert(2 + 2 == 4, "weird arithmetic")
    

    Only, because assert is a macro and not a method, the boolean expression 2 + 2 == 4 will be evaluated only if assertions were enabled. Note that there exists a shorthand to help with expressing AST’s, but this example is hopefully clearer this way.

    Last but not least – Scala 2.10 macros will be a part of Scala proper – Integrated into the standard distribution –
    as opposed to provided by some third-party library.

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

Sidebar

Related Questions

Don't know if anyone can help me with this or if it's even possible.
don't know if this is possible.. I'm using sqlite3 schema: CREATE TABLE docs (id
Don't know why but I can't find a solution to this. I have 3
Don't know why this doesn't work. I can't do implicit animation for a newly
I don't know why, but this code worked for me a month ago... maybe
I don't know how better to describe this. So take this example. var a
Don't have much to say, just can get into the event handler. XAML: <Grid>
Don't know if this is the right place to ask this, but I will
Don't know why this is happening, but after submitting a form via JS (using
(Don't know if this is strictly on-topic, but I don't see any better Stack

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.