ILoop addThunk & adding imports

321 views
Skip to first unread message

pingel

unread,
May 18, 2014, 7:08:42 PM5/18/14
to scala-i...@googlegroups.com

I noticed that ILoop's addThunk is no longer available in 2.11

In 2.10 I was extending ILoop to create a custom REPL.  I used addThunk to include a list of imports available in the repl by default:

class MyILoop extends ILoop {

override def prompt = ...

override def printWelcome() = ...

  addThunk { intp.beQuietDuring { intp.addImports("some.package._", ...) } }

}

What is the right way to do this now?  I've tried extending existing methods (with something like intp.interpret("import some.package._")) but this always seems to catch intp before it is fully initialized.

Thanks!

Som Snytt

unread,
May 18, 2014, 9:15:28 PM5/18/14
to scala-internals
There's `-D"scala.repl.autoruncode"`; there's also `scala -i` or `ILoop.loadFiles` but there are known issues with the multithreaded start-up.

sbt has a similar conundrum about executing `initialCommands in console`.

OK, so `$ scala -i ~/init.script` breaks autocompletion, but handles commands like :paste.

`$ scala -Dscala.repl.autoruncode=start.script` will do imports and doesn't break autocompletion.

I hadn't tried it before, thank you very much.





--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David van Geest

unread,
Sep 11, 2015, 3:27:40 PM9/11/15
to scala-internals
I realize this is quite an old thread, but I came across it when having the same problem as the original poster.

My solution was to override `createInterpreter` like so:

class Console extends ILoop {
 
private val initialCommands = """
    import com.example._
    """



 
override def createInterpreter: Unit = {
   
super.createInterpreter
    intp
.interpret(initialCommands)
 
}
}

Seems to work fine, but if there's a better way I would be glad to hear it. I took the idea from the SBT source.

David

som-snytt

unread,
Sep 11, 2015, 6:39:00 PM9/11/15
to scala-internals
The only downside is that it's early in intp init. I remember from the time of my previous post:

http://stackoverflow.com/a/23646271/1296806

where the issue was that "$intp" isn't available yet from sbt. I see the context classloader isn't set yet.

I once rejiggered the startup and provided a nicer hook, but currently, I see a sample from last January:

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

object Test extends App {
  val foo
= 42
  val code
= """
    import concurrent._, ExecutionContext.Implicits._
    $intp.getContext.getScopes
    val f = Future(8)
    foo
    f
  """
.lines.toList
  val repl
= new ILoop {
   
override def printWelcome() = {
      processLine
("")
      intp
.bind("foo", foo)
      code
foreach intp.interpret
     
super.printWelcome()
      echo
("Customized...")
   
}
 
}
  val s
= new Settings
  s
.usejavacp.value = true
  s
.Xnojline.value  = true
  repl process s
}

The call to `processLine` tries to make sure the compiler is initialized.
Reply all
Reply to author
Forward
0 new messages