scala as scripting language [duplicate]
Asked Answered
F

2

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.

Flavin answered 28/5, 2011 at 20:19 Comment(5)
Questions? I call that assignments.Honeycomb
@delnan how can you possibly think this is homework?Novelize
@ninjagecko: I don't think its OP's homework. But the way this "question" is stated, it reads as if it was out homework.Honeycomb
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. @Novelize 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.Flavin
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.Flavin
F
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
}
Flavin answered 28/5, 2011 at 22:0 Comment(2)
A bit late maybe, but settings.embeddedDefaults(this.getClass().getClassLoader()) before initializing IMain will probably fix this problem.Cromorne
@Cromorne I had similar problem of casting and I used your solution. But now I am getting internalcompilerexception #52966497. Can you please help me out in solving thisAsmodeus
N
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.)

Novelize answered 28/5, 2011 at 20:24 Comment(1)
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.Flavin

© 2022 - 2024 — McMap. All rights reserved.