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

compile in app

4 replies
Michael Fortin
Joined: 2010-02-16,
User offline. Last seen 42 years 45 weeks ago.

Is there an example somewhere of how to compile scala code from inside an application? I was looking at scala.tools.nsc.Main but I can't find any documentation or scaladoc. The only info I could find about the parameters was my calling '> scalac -help'. A small example would be very helpful.

Thanks,
_M!ke

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: compile in app
have you tried stack-overflow?

On 9 July 2010 19:11, Michael Fortin <michael [at] m410 [dot] us> wrote:
Is there an example somewhere of how to compile scala code from inside an application?  I was looking at scala.tools.nsc.Main but I can't find any documentation or scaladoc.  The only info I could find about the parameters was my calling '> scalac -help'.  A small example would be very helpful.

Thanks,
_M!ke




--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: compile in app

On Friday July 9 2010, Kevin Wright wrote:
> have you tried stack-overflow?

Stack overflow can't do that. It can only serve as a repository for and
source of knowledge. ... Oh, I get it!

> On 9 July 2010 19:11, Michael Fortin wrote:
> > Is there an example somewhere of how to compile scala code from
> > inside an application? I was looking at scala.tools.nsc.Main but I
> > can't find any documentation or scaladoc. The only info I could
> > find about the parameters was my calling '> scalac -help'. A small
> > example would be very helpful.
> >
> > Thanks,
> > _M!ke

RRS

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: compile in app
That's not necessarily easy to do. While I do know this question has been asked before, I, for one, couldn't find it right now.

On Fri, Jul 9, 2010 at 3:30 PM, Kevin Wright <kev [dot] lee [dot] wright [at] gmail [dot] com> wrote:
have you tried stack-overflow?

On 9 July 2010 19:11, Michael Fortin <michael [at] m410 [dot] us> wrote:
Is there an example somewhere of how to compile scala code from inside an application?  I was looking at scala.tools.nsc.Main but I can't find any documentation or scaladoc.  The only info I could find about the parameters was my calling '> scalac -help'.  A small example would be very helpful.

Thanks,
_M!ke




--
Kevin Wright

mail/google talk: kev [dot] lee [dot] wright [at] gmail [dot] com
wave: kev [dot] lee [dot] wright [at] googlewave [dot] com
skype: kev.lee.wright
twitter: @thecoda




--
Daniel C. Sobral

I travel to the future all the time.
Johannes Rudolph 2
Joined: 2010-02-12,
User offline. Last seen 42 years 45 weeks ago.
Re: compile in app

Here are two examples:

http://people.apache.org/~mduerig/scala4scripting/

You might probably want to watch his talk from the Scala Days as well.

Here's some code I wrote some time ago (in Java!) which uses Scala for
scripting (I'm not sure which version but definitely pre-2.8):

http://github.com/jrudolph/scolorz/blob/master/src/net/virtualvoid/graph...

Actually it is quite easy to use the Scala interpreter (beware Java
code following):

import scala.tools.nsc.Interpreter;
import scala.tools.nsc.Settings;

final Interpreter interpreter = new Interpreter(new Settings());
final Painter[] painter = new Painter[1];

interpreter.bind("painter", "Array[net.virtualvoid.graphics.Painter]", painter);

String code = "object aPainter extends net.virtualvoid.graphics.Painter{\n"
+ "def paint:Unit = {\n"
+ text.getText() + "\n}\n"
+ "}\n";
interpreter.interpret(code);
interpreter.interpret("painter(0) = aPainter;");

So I defined a Painter interface and bound an array of that variable
to receive the results. Code which I executed got wrapped by code
which implements the interface. After evaluating this, the result is
stored into the array and can be accessed afterwards.

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