是否有 Scala 版本的 .irbrc 或其他方法来定义一些默认库供 REPL 使用?

发布于 2024-09-05 19:49:44 字数 739 浏览 3 评论 0原文

我编写了一个小库,它使用隐式添加仅在 Scala 中使用 REPL 时才需要的功能。 Ruby 有这样的库 - 例如漂亮的打印、启动文本编辑器(例如从 irb 调用 Vim 的 Interactive_editor gem - 请参阅 这篇文章)、调试器等。我正在尝试编写的库使用“pimp my library”隐式转换过程向 java.lang.Class 和 java.lang.reflect 类添加了一些方法,以帮助您查找文档(最初是使用 Google,然后可能是使用JavaDoc/ScalaDoc 查看器,最终可能还有 StackOverflow API!)。这是一个令人抓狂的库:我花了很多时间将类名复制并粘贴到 Google 中,我想我也可以自动化这个过程。

这是开发人员希望添加到他们的系统中以便仅在 REPL 中使用的功能 - 他们不应该真正将其添加到项目中(部分原因是它可能不是他们的开发人员同事想要的东西,而且还因为如果如果您正在进行一些探索性开发,可能只是使用 Scala REPL,而 IDE 或构建工具并未调用)。

就我而言,我想包含一些类并设置一些隐式 - 在 CLASSPATH 中包含一个 .jar 并导入它,基本上。

在 Ruby 中,您需要添加到 .irbrc 文件中。其他 REPL 也有类似的设置选项和导入库的方法。

Scala REPL 是否有类似的文件或方法可以做到这一点?

I've written a little library that uses implicits to add functionality that one only needs when using the REPL in Scala. Ruby has libraries like this - for things like pretty printing, firing up text editors (like the interactive_editor gem which invokes Vim from irb - see this post), debuggers and the like. The library I am trying to write adds some methods to java.lang.Class and java.lang.reflect classes using the 'pimp my library' implicit conversion process to help you go and find documentation (initially, with Google, then later possibly with a JavaDoc/ScalaDoc viewer, and maybe the StackOverflow API eventually!). It's an itch-scratching library: I spend so much time copying and pasting classnames into Google that I figured I may as well automate the process.

It is the sort of functionality that developers will want to add to their system for use only in the REPL - they shouldn't really be adding it to projects (partly because it may not be something that their fellow developers want, but also because if you are doing some exploratory development, it may be with just a Scala REPL that's not being invoked by an IDE or build tool).

In my case, I want to include a few classes and set up some implicits - include a .jar on the CLASSPATH and import it, basically.

In Ruby, this is the sort of thing that you'd add to your .irbrc file. Other REPLs have similar ways of setting options and importing libraries.

Is there a similar file or way of doing this for the Scala REPL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小霸王臭丫头 2024-09-12 19:49:44

在命令行上,您可以使用 -i 选项在启动 REPL 时加载文件:

scala -cp mystuff.jar -i mydefs.scala

当然,您可以将其包装在 shell 脚本或批处理文件中并运行它,而不是正常的 scala 命令。

(我使用的是 Scala 2.8.0 RC3)。

On the command line, you can use the -i option to load a file while starting the REPL:

scala -cp mystuff.jar -i mydefs.scala

Ofcourse you could wrap this in a shell script or batch file and run that instead of the normal scala command.

(I'm using Scala 2.8.0 RC3).

画中仙 2024-09-12 19:49:44

不确定这是否是您正在寻找的内容,但是您是否将任何 jar 放入您的 SCALA_HOME\lib 目录中。然后这些 jar 就可以在 REPL 中导入(使用 import 关键字)。

编辑:目前最方便的选项是设置 CLASSPATH 环境变量。 CLASSPATH 变量中引用的任何 jar 也可在 REPL 中导入。

Not sure if this is what you are looking for, but if you put any jars in your SCALA_HOME\lib directory. Then those jars will be available for import in the REPL (using the import keyword).

EDIT: The most convenient option as of now is by setting the CLASSPATH environment variable. Any jars referenced in the CLASSPATH variable are also available for import in the REPL.

冰魂雪魄 2024-09-12 19:49:44

快速回答可能不是您想要的,但是

输入:load path/to/some/scala/script/file.scala

在控制台中

怎么样? :load 将读入 scala 文件并将其作为脚本执行。

另一种选择是使用 sbt 设置依赖项并执行控制台命令。

我能想到的最后一个选项是在命令行上手动设置类路径并将其指向您希望 jvm 了解的 jars/class 文件夹。

如果您对此感兴趣,请告诉我,如果需要,我可以提供更多详细信息。

Quick answer probably not what you are looking for, but what about typing

:load path/to/some/scala/script/file.scala

in the console?

:load will read in a scala file and execute it as a script.

Another option is to use sbt set up your dependencies and execute the console command.

The final option I can think of is to set the classpath on the command line manually and point it to the jars / class file folders that you want the jvm to know about.

Let me know if any of this interests you and I can provide more details if needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文