2012-09-27 4 views
6

-Hi. Je voudrais intégrer Scala REPL avec environnement initialisé dans mon application. J'ai regardé IMain classe et il semble que je pourrais le faire via l'instance de celui-ci. L'instance est créée, puis stockée dans intp public var dans process() de ILoop.Scala - Initialise l'environnement REPL

Comment puis-je lier certains noms et/ou ajouter des importations avant process() (par exemple, avant REPL)?

code suivant échoue sur la ligne 3, car intp est pas encore créé (=> NPE):

val x = 3 
    val interp = new ILoop 
    interp.bind("x", x) // -> interp.intp.bind("x", x) 
    val settings = new Settings 
    settings.usejavacp.value = true 
    interp.process(settings) 

vous- Merci.

MISE À JOUR: Redéfinition createInterpreter() ne fonctionne malheureusement pas:

val x = 3 
    val interp = new ILoop { 
     override def createInterpreter() { 
      super.createInterpreter() 
      intp.bind("x", x) // -> interp.intp.bind("x", x) 
     } 
    } 
    val settings = new Settings 
    settings.usejavacp.value = true 
    interp.process(settings) 

interprète est bloqué sur l'entrée (qui ressemble à une impasse, se produit uniquement avec le code ci-dessus):

x: Int = 3 
Failed to created JLineReader: java.lang.NoClassDefFoundError: scala/tools/jline/console/completer/Completer 
Falling back to SimpleReader. 
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_06-icedtea). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> println 
<infinite_sleep> 

Merci pour dvigal suggestion.

Répondre

4

Il y a un projet github appelé scala-ssh-shell qui peut faire ce que vous voulez, ou au moins vous rapprocher.

+0

J'ai regardé le projet et il semble que ça va marcher. Je vous remercie. – woky

1

-Bonjour, désolé je ne pirate Scala REPL mais je pense que vous pouvez faire quelque chose comme:

class YourILoop(in0: Option[BufferedReader], protected override val out: JPrintWriter)   
    extends ILoop(in0, out) { 
    override def createInterpreter() { 
     if (addedClasspath != "") 
      settings.classpath append addedClasspath 

      intp = new ILoopInterpreter 
      val x = 3; 
      intp.bind("x", x) 
    } 
} 
object Run { 
    def errorFn(str: String): Boolean = { 
     Console.err println str 
     false 
    } 

    def process(args: Array[String]): Boolean = { 
     val command = new GenericRunnerCommand(args.toList, (x: String) => errorFn(x)) 
     import command.{ settings, howToRun, thingToRun } 
     new YourILoop process settings 
    } 
    def main(args: Array[String]) { 
     process(args) 
    } 
} 
+0

bon, merci, malheureusement, cela ne fonctionne pas, j'ai mis à jour la réponse – woky

Questions connexes