10

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.

Community
  • 1
  • 1
Arne
  • 7,921
  • 9
  • 48
  • 66
  • 1
    Questions? I call that assignments. –  May 28 '11 at 20:21
  • @delnan how can you possibly think this is homework? – ninjagecko May 28 '11 at 20:23
  • @ninjagecko: I don't think its OP's homework. But the way this "question" is stated, it reads as if it was out homework. –  May 28 '11 at 21:13
  • ok I changed the last sentence. I stated all I need to do in this "Homework" style manner, because I think thats the most compact way to describe my problem. @ninjagecko you are right, this thread is slightlich redundant to "'evel' in Scala", but I doublt that the last problem is solveable in any eval-ish scripting manner, and that is my most important one. – Arne May 29 '11 at 00:41
  • is it possible to reopen this thread, or do I have To ask my question again, util I get an answer? Because the most important task with the redefenition and reloading of a Class is not solved at all. – Arne May 29 '11 at 14:43

2 Answers2

5

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
}
Arne
  • 7,921
  • 9
  • 48
  • 66
  • A bit late maybe, but `settings.embeddedDefaults(this.getClass().getClassLoader())` before initializing IMain will probably fix this problem. – Jasper-M Jun 16 '14 at 23:32
  • @Jasper-M I had similar problem of casting and I used your solution. But now I am getting internalcompilerexception https://stackoverflow.com/questions/52965997/internalcompilerexception-compiling-class-was-loaded-through-a-different-loader. Can you please help me out in solving this – user811602 Oct 24 '18 at 12:32
1

See my answer here: "eval" in Scala

(I assume you are already aware of scala interactive mode, and how to write executable scripts with a Scala-compatible shebang #! line.)

Community
  • 1
  • 1
ninjagecko
  • 88,546
  • 24
  • 137
  • 145
  • yes interactive mode were my first steps and I do also know how to write those scripts, but that is not my intention, my Intention is more in the direction of scripting languages in games. – Arne May 28 '11 at 21:55