内部 DSL 的 Scala REPL 个性化

Scala REPL personalization for internal DSL

关于为内部 DSL 个性化 Scala REPL,来自 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._")
    }
  }
}

注意到 addThunk 在 2.11.* 中不受支持。

那么如何将 myDSL.jarimport my.dsl._ 加载到个性化的 REPL 中?

您可以将初始化代码粘贴到文件中,类似于“-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 $  "
}

或者:

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
}