2

On personalizing Scala REPL for internal DSL, from create-your-custom-scala-repl

import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.ILoop

object TestConsole extends App {
  val settings = new Settings
  settings.usejavacp.value = true
  settings.deprecation.value = true

  new SampleILoop().process(settings)
}

class SampleILoop extends ILoop {
  override def prompt = "myDSL $  "

  addThunk {
    intp.beQuietDuring {
      intp.addImports("my.dsl._")
    }
  }
}

noticed that addThunk is not supported in 2.11.* .

Thus how to load myDSL.jar or import my.dsl._ into a personalized REPL ?

elm
  • 20,117
  • 14
  • 67
  • 113

1 Answers1

4

You can stick init code in a file, similar to "-i":

import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.ILoop

object TestConsole extends App {
  val settings = new Settings
  settings.usejavacp.value = true
  settings.deprecation.value = true

  new sys.SystemProperties += ("scala.repl.autoruncode" -> "myrepl.init")

  new SampleILoop().process(settings)
}

class SampleILoop extends ILoop {
  override def prompt = "myDSL $  "
}

Or:

object TestConsole extends App {
  val settings = new Settings
  settings.usejavacp.value = true
  settings.deprecation.value = true

  new sys.SystemProperties += (
    "scala.repl.autoruncode" -> "myrepl.init",
    "scala.shell.prompt" -> "myDSL $ "
  )

  new scala.tools.nsc.interpreter.ILoop process settings
}
som-snytt
  • 39,429
  • 2
  • 47
  • 129
  • Many Thanks; how would the contents of `myrepl.init` as a replacement of `intp.addImports("my.dsl._")` ? – elm Jan 02 '15 at 17:44
  • Each line of the file is run as `intp.interpret`; looks like those autorun commands go in command history. – som-snytt Jan 02 '15 at 18:23
  • Tried command `scala TestConsole.jar -cp myDSL.jar` ; also in `myrepl.init` added `import my.dsl._` yet could not get DSL related classes... – elm Jan 02 '15 at 18:39
  • By inserting (terse) DSL code in myrepl.init it works. – elm Jan 02 '15 at 18:57
  • I also refreshed my memory how to do it with sbt. Use `settings.embeddedDefaults` and don't set `usejavacp`. http://stackoverflow.com/a/25331737/1296806 – som-snytt Jan 02 '15 at 19:09
  • The "extends App" form only works for me if I compile it and then run it in two steps. Treating this as a script and running directly with "scala" doesn't work for me; I had to replace the "extends App" with an explicitly defined main method. – jbyler Sep 04 '15 at 20:25