在 Scala REPL 中导入多个包 [英] Import multiple packages in Scala REPL

查看:45
本文介绍了在 Scala REPL 中导入多个包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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(...)
...

我知道无法在 Scala 中导入多个包(尽管包对象范围可以提供一些帮助),但是从 REPL 中呢?有没有办法从 REPL 中做到这一点而无需大量复制和粘贴?我也尝试使用 :load,但在那里导入/创建的东西也没有被引入 REPL 的范围.我注意到 :power 命令确实将内容导入范围.

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.

更新::load 实际上确实有效.

推荐答案

我不确定通用的解决方案,但在 SBT 中可以配置项目,以便控制台 (REPL) 在启动时运行一些初始命令.这是示例build.sbt文件的摘录,>

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._"

编辑:另一个参考是 Scalala 项目.他们有一个 Scala 程序,可以启动嵌入式 REPL 并填充在必要的进口.有关用法,请参阅他们的快速入门指南.

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.

这篇关于在 Scala REPL 中导入多个包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆