Scala Presentation Compiler - Minimal Example -


can supply minimal example of using current scala presentation compiler (i.e. scala.tools.nsc.interactive.global), accomplishes following tasks?

  • compile single virtual source file (i.e. not in file system e.g. string)
  • retrieve resulting information phases compiler
  • propagate change in source file compiler
  • receive further information possibly asynchronously

there seems lot of fluctuation in nsc , couldn't find date small examples. highly appreciate here.

ok, 1 week of 100 bounty , still no answers try myself... edits very welcome!

the key class presentation compiler scala.tools.nsc.interactive.global. start, need create instance of compiler.

 import scala.tools.nsc.interactive.global
class presentationcompiler { // want compiler output virtual val target = new virtualdirectory("", none)
// need adjusted in order work // sbt. see this question. val settings = new settings() // output virtual target settings.outputdirs.setsingleoutput(target)
// can replaced custom instance // of abstractreporter gain control. val reporter = new consolereporter(settings)
val compiler = new global(settings, reporter)
... }

for settings the link provided abhishek valuable.

but interesting part:

1. compile single virtual source file

to compile string there possibility create batchsourcefile underlying virtualfile. api marked experimental here , seems patchy.

def compile(code: string) {   val source = new batchsourcefile("<virtual>", code)   val response = new response[unit]   compiler.askreload(list(source), response)       response.get.left.foreach { _ =>     // success   } } 

2. retrieve resulting information phases compiler

this tricky part. due multi-threaded nature of compiler , fact flags reused different meanings across diferent phases, impossible @ once. have resort asksomething kind of methods documented in api. example:

val tcompletion = new response[list[global.member]]       val pos = compiler.ask(() => new offsetposition(source, p)) global.asktypecompletion(pos, tcompletion) tcompletion.get(5000).get match {   case left(members) => // members   case right(e) =>     e.printstacktrace } 

3. propagate change in source file compiler

this interesting part, wanted find out question. dont this, because batchsourcefile described file which's content not change on time. custom implementation of sourcefile need supplied?? why not in interactive package. sure didn't catch something.

so solution right call compile method again.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -