This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Using Scala script from Scala program

1 reply
Samuel Robert Reid
Joined: 2008-12-17,
User offline. Last seen 1 year 22 weeks ago.
After designing a scala-based DSL, I'd like to be able to use code written in the DSL in Scala programs without having it be part of the scala compilation unit.  The most convenient would be to be able to do something like this:
var resultFromScript=ScalaScript.evaluate("/path/to/scala/script.scala")
Does the Scala API provide a way of doing this, or will I need to implement my own support for this, possibly using reflection/scala compiler/class loading?  Alternatively, if there's a better way of solving the overall problem (or arguments for putting the DSL scripts as part of the compilation unit), I'd be happy to hear about it.

Thanks,
Sam Reid
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Using Scala script from Scala program

On Thu, Mar 12, 2009 at 10:38:53AM -0600, Samuel Robert Reid wrote:
> After designing a scala-based DSL, I'd like to be able to use code
> written in the DSL in Scala programs without having it be part of the
> scala compilation unit. The most convenient would be to be able to do
> something like this:
>
> var resultFromScript=ScalaScript.evaluate("/path/to/scala/script.scala")
>
> Does the Scala API provide a way of doing this, or will I need to
> implement my own support for this, possibly using reflection/scala
> compiler/class loading?

Anything involving reflection and classloaders would be major overkill,
since even if there were no scala interface you could just use
Runtime.exec to run it. However, the compiler provides a whole bunch of
ways to do this -- too many in fact -- and I'm not at all sure what the
recommended way is. I want to see about specifying a consistent
interface, but I don't know when I'll get to it.

In the meantime start with scala.tools.nsc and look at MainGenericRunner
and ScriptRunner, and if those don't do what you want look through the
rest of the same package.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland