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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:04:30+00:00 2026-05-22T17:04:30+00:00

Possible Duplicate: “eval” in Scala I know scala is a compiled language, but I

  • 0

Possible Duplicate:
“eval” in Scala

I know scala is a compiled language, but I do also know that I can dynamically load classes into the jvm, and I can call the scala compiler at runtime, last but not least I do also have an awesome repl, so having scala as a scripting language should be possible.

so there are some tasks I need to get to run:

simple interpret:

val src = """ println("Hello World") """
interpret(src)

call external functions:

object A{
    def foo = 
        println("Hello World")
}

val src = """ A.foo """
interpret(src)

implement functionality:

trait T{
    def foo:String
}

val src = """ class A extends T{ def foo = "Hello World" } """
interpret(src)
val t = loadClassAndCreatInstance.asInstanceOf[T]
println(t.foo)

it would be great to get a solution to all my problems.

  • 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-22T17:04:31+00:00Added an answer on May 22, 2026 at 5:04 pm

    somehow I already found out how to use scala as scripting language but I still have a problem with the classLoader

    object O{
      def foo = println("Hello World in object O")
    }
    
    trait T{
      def foo:String
    }
    
    object MyInterpreter extends App{
      val srcA = 
      """ 
      println("Hello World from srcA") 
      """
    
      val srcB = """ O.foo """
    
      val srcC = """ 
      class A extends T{ 
        def foo = "Hello World from srcC"
        override def toString = "this is A in a src"
      }
      """
    
    
      val out = System.out
      val flusher = new java.io.PrintWriter(out)
    
      val interpreter = {
      val settings = new import scala.tools.nsc.GenericRunnerSettings( println _ )
      new scala.tools.nsc.interpreter.IMain(settings, flusher)
      }
    
      interpreter.interpret(srcA)
      interpreter.interpret(srcB)
      interpreter.compileString(srcC)
    
      val classA = interpreter.classLoader.findClass("A")
    
      println(classA)
    
      val constructors = classA.getDeclaredConstructors
      val myinstance = constructors(0).newInstance()
    
      println(myinstance)
    
      //this still throws an classCastException
      myinstance.asInstanceOf[T].foo 
      //but everything else works
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: “ResizeEnd” equivalent for usercontrols I feel stupid, but I can't find a
Possible Duplicate: Why are two different concepts both called “heap”? I've googled around, but
Possible Duplicate: “main” function in Lua? In Python, you can check if a script
Possible Duplicate: What does “String[] args” contain in java? I want to know the
Possible Duplicate: Scala “<-” for comprehension Could someone please explain how the <- operator
Possible Duplicate: Are “(function ( ) { } ) ( )” and “(function (
Possible Duplicate: Weird “406 not acceptable” error following code generate a 406 Not Acceptable
Possible Duplicate: ASP.NET “special” tags What is the difference between <%# ... %> ,
Possible Duplicate: When to use “strictfp” keyword in java? What is the use of

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.