Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

read eval print loop - Import multiple packages in Scala REPL

In Scala, I frequently have to import multiple packages worth of implicits and other utilities, particularly on the REPL:

import scala.collection.JavaConversions._
import scala.collection.{mutable => mut}
import com.myapp.db._
import com.orm._
val con = connectDb(...)
...

I understand there's no way to import multiple packages in Scala (though package object scopes can help a bit), but what about from the REPL? Is there any way to do this from the REPL without lots of copying and pasting? I also tried using :load, but things imported/created there don't get introduced into the REPL's scope either. I noticed that the :power command does import things into scope.

Update: :load actually does work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not sure of a general solution, but in SBT it's possible to configure the project so that the console (REPL) runs some initial commands upon launching. Here's an excerpt from an example build.sbt file,

// set the initial commands when entering 'console' only
initialCommands in console := "import myproject._"

Edit: Another reference is the Scalala project. They have a Scala program that launches an embedded REPL and fills in the necessary imports. For usage, see their quick-start guide.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...