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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:45:38+00:00 2026-06-10T07:45:38+00:00

In Scala 2.10 how do I generate a class from string (probably, using the

  • 0

In Scala 2.10 how do I generate a class from string (probably, using the Toolbox api) later to be instantiated with Scala’s reflection?

  • 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-10T07:45:40+00:00Added an answer on June 10, 2026 at 7:45 am

    W.r.t compilation toolboxes can only run expressions = return values, but not resulting classes or files/byte arrays with compilation results.

    However it’s still possible to achieve what you want, since in Scala it’s so easy to go from type level to value level using implicit values:

    Edit. In 2.10.0-RC1 some methods of ToolBox have been renamed. parseExpr is now just parse, and runExpr is now called eval.

    scala> import scala.reflect.runtime._ // requires scala-reflect.jar
                                          // in REPL it's implicitly added 
                                          // to the classpath
                                          // but in your programs
                                          // you need to do this on your own
    import scala.reflect.runtime
    
    scala> val cm = universe.runtimeMirror(getClass.getClassLoader)
    cm @ 41d0fe80: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain$TranslatingClassLoader...
    
    scala> import scala.tools.reflect.ToolBox // requires scala-compiler.jar
                                              // in REPL it's implicitly added 
                                              // to the classpath
                                              // but in your programs
                                              // you need to do this on your own
    import scala.tools.reflect.ToolBox
    
    scala> val tb = cm.mkToolBox()
    tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@3a962da5
    
    scala> tb.runExpr(tb.parseExpr("class C; scala.reflect.classTag[C].runtimeClass"))
    res2: Any = class __wrapper$1$f9d572ca0d884bca9333e251c64e980d$C$1
    

    Update #1. If you don’t need a java.lang.Class and just need to instantiate the compiled class, you can write new C directly in the string submitted to runExpr.

    Update #2. It is also possible to have runExpr use custom mapping from variable names to runtime values. For example:

    scala> val build = scala.reflect.runtime.universe.build
    build: reflect.runtime.universe.BuildApi = scala.reflect.internal.BuildUtils$BuildImpl@50d5afff
    
    scala> val x = build.setTypeSignature(build.newFreeTerm("x", 2), typeOf[Int])
    x: reflect.runtime.universe.FreeTermSymbol = free term x
    
    scala> tb.runExpr(Apply(Select(Ident(x), newTermName("$plus")), List(Literal(Constant(2)))))
    res0: Any = 4
    

    In this example I create a free term that has a value of 2 (the value doesn’t have to be a primitive – it can be your custom object) and bind an identifier to it. This value is then used as-is in the code that is compiled and run by a toolbox.

    The example uses manual AST assembly, but it’s possible to write a function that parses a string, finds out unbound identifiers, looks up values for them in some mapping and then creates corresponding free terms. There’s no such function in Scala 2.10.0 though.

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

Sidebar

Related Questions

Since I want to generate Scala code from an annotated Scala class, I need
I want to generate a dynamic SQL where clause from a Scala collection, such
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
If I generate some Javascript in my Scala code like this: <script type=text/javascript> foo({bar});
In Scala it's possible to use the annotation @BeanProperty to automatically generate getters and
I'm using Netbeans to write Scala and Java. Netbeans generated a .jar file for
scala> class A defined class A scala> trait B defined trait B Creating an
Say you define the following: class Person(name: String, age: Int) { def toXml =
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
In Scala, a class's primary constructor has no explicit body, but is defined implicitly

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.