Skip to content

Instantly share code, notes, and snippets.

@mmhelloworld
Last active December 18, 2015 04:39
Show Gist options
  • Select an option

  • Save mmhelloworld/5726685 to your computer and use it in GitHub Desktop.

Select an option

Save mmhelloworld/5726685 to your computer and use it in GitHub Desktop.
Embed Scala REPL
object InterpreterTest {
import scala.tools.nsc._
import interpreter._
def break(params: NamedParam*): Unit = {
val repl = new ILoop
repl.settings = new Settings
repl.settings.Yreplsync.value = true
repl.in = SimpleReader()
repl.createInterpreter
repl.intp.beQuietDuring {
params.map(repl.intp.bind)
}
repl.loop
repl.closeInterpreter
}
def breakIf(cond: => Boolean, params: NamedParam*): Unit =
if (cond) break(params: _*)
def main(args: Array[String]) {
0 to 10 foreach { i =>
breakIf(i == 5, NamedParam("j", i))
println(i)
}
}
}
/*
* Output:
$ scala -cp bin InterpreterTest
0
1
2
3
4
scala> j
res0: Int = 5
scala> :q
5
6
7
8
9
10
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment