tags:

views:

74

answers:

2

I am trying to retrieve the AST from scala souce file. I have simplified the code (only relevant code) to following.

trait GetAST {
     val settings = new Settings
     val global = new Global(settings, new ConsoleReporter(settings))
     def getSt = "hello" //global.typedTree(src, true)
}
object Tre extends GetAST {
    def main(args:Array[String])
    {
        println(getSt.getClass)
        println("exiting program")
    }
}

The above code compiles fine and runs fine. But the problem is the program does not exit. The prompt is not displayed after printing "exiting program". I have to use ^c to exit. Any idea what the problem might be

+2  A: 

Without knowing what Settings, Global and ConsoleReporter are nobody can give you an exact answer. I would guess that at least one of them is creating a thread. The JVM waits until all threads are done (or all running are deamon threads). See here.

I would bet if you comment out the settings and global lines it will exit as expected.

michael.kebe
Settings, Global and ConsoleReporter are from standard scala compiler.import scala.tools.nsc.Settingsimport scala.tools.nsc.interactive.Globalimport scala.tools.nsc.reporters.ConsoleReporter
scout
+2  A: 

I believe Michael is correct, the compiler uses Threads and therefore the JVM doesn't just exit.

The good news is that interactive.Global mixes in interactive.CompilerControl trait whose askShutdown method you can call at the end of your main to let the program exit.

Mirko Stocker
That was exactly what I was missing. Thanks
scout