如何在 Scala REPL 中使用第三方库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18812399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:40:15  来源:igfitidea点击:

How to use third party libraries with Scala REPL?

scalainterpreterread-eval-print-loop

提问by Harshal Kshatriya

I've downloaded Algebirdand I want to try out few things in the Scala interpreterusing this library. How do I achieve this?

我已经下载了Algebird,我想使用这个库在Scala 解释器中尝试一些东西。我如何实现这一目标?

回答by Rüdiger Klaehn

Of course, you can use scala -cp whatever and manually manage your dependencies. But that gets quite tedious, especially if you have multiple dependencies.

当然,您可以使用任何 scala -cp 并手动管理您的依赖项。但这会变得非常乏味,尤其是当您有多个依赖项时。

A more flexible approach is to use sbtto manage your dependencies. Search for the library you want to use on search.maven.org. Algebird for example is available by simply searching for algebird. Then create a build.sbt referring to that library, enter the directory and enter sbt console. It will download all your dependencies and start a scala console session with all dependencies automatically on the classpath.

更灵活的方法是使用sbt来管理您的依赖项。在search.maven.org上搜索您要使用的库。例如,只需搜索 algebird即可获得 Algebird。然后创建一个引用该库的 build.sbt ,进入目录并输入sbt console。它将下载您的所有依赖项,并在类路径上自动启动包含所有依赖项的 Scala 控制台会话。

Changing things like the scala version or the library version is just a simple change in the build.sbt. To play around you don't need any scala code in your directory. An empty directory with just the build.sbt will do just fine.

更改 scala 版本或库版本之类的内容只是 build.sbt 中的一个简单更改。玩转您的目录中不需要任何 Scala 代码。一个只有 build.sbt 的空目录就可以了。

Here is a build.sbtfor using algebird:

这里是一个build.sbt使用algebird:

name := "Scala Playground"

version := "1.0"

scalaVersion := "2.10.2"

libraryDependencies += "com.twitter" % "algebird-core" % "0.2.0"

Edit: often when you want to play around with a library, the first thing you have to do is to import the namespace(s) of the library. This can also be automated in the build.sbt by adding the following line:

编辑:通常当您想使用库时,您要做的第一件事就是导入库的命名空间。这也可以通过添加以下行在 build.sbt 中自动化:

initialCommands in console += "import com.twitter.algebird._"

回答by Zoltán

Running sbt consolewill not import libraries declared with a test scope. To use those libraries in the REPL, start the console with

运行sbt console不会导入使用测试范围声明的库。要在 REPL 中使用这些库,请使用以下命令启动控制台

sbt test:consoleQuick

You should be aware, however, that starting the console this way skips compiling your test sources.

但是,您应该知道,以这种方式启动控制台会跳过编译您的测试源。

Source: http://www.scala-sbt.org/0.13/docs/Howto-Scala.html

来源:http: //www.scala-sbt.org/0.13/docs/Howto-Scala.html

回答by S.R.I

You can use the scala's -cpswitch to keep jars on the classpath. There are other switches available too, for example, -deprecationand -uncheckedfor turning on various warnings. Many more to be found with scala -X...and scala -Y.... You can find out more information about these switches with scala -help

您可以使用 Scala 的-cp开关将 jars 保留在类路径上。还有其他的交换机也可以,例如, -deprecation以及-unchecked用于打开各种警告。还有更多可以通过scala -X...和找到scala -Y...。您可以通过以下方式找到有关这些开关的更多信息scala -help

回答by Valy Dia

This is an answer using Ammonite(as opposed to the Scala REPL) - but it is such a great tool that it is worth mentioning.

这是使用Ammonite(而不是 Scala REPL)的答案- 但它是一个非常棒的工具,值得一提。

  1. You can install it with a one liner such as:
  1. 您可以使用一个衬垫安装它,例如:
sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/2.1.2/2.13-2.1.2) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm

or using brew on macOS:

或在 macOS 上使用 brew:

brew install ammonite-repl  

For scala 2.10, you need to use an oder version 1.0.3:

对于 Scala 2.10,您需要使用 1.0.3 版本:

sudo sh -c '(echo "#!/usr/bin/env sh" && curl -L https://github.com/lihaoyi/Ammonite/releases/download/1.0.3/2.10-1.0.3) > /usr/local/bin/amm && chmod +x /usr/local/bin/amm' && amm
  1. Run Ammonite in your terminal:
  1. 在终端中运行 Ammonite:
amm
// Displays
Loading...
Welcome to the Ammonite Repl 2.1.0 (Scala 2.12.11 Java 1.8.0_242)
  1. Use in ivy import to import your 3rd part library:
  1. 在常春藤导入中使用以导入您的第三部分库:
import $ivy.`com.twitter::algebird-core:0.2.0`

Then you can use your library within the Ammonite-REPL:

然后你可以在 Ammonite-REPL 中使用你的库:

import com.twitter.algebird._
import com.twitter.algebird.Operators._
Map(1 -> Max(2)) + Map(1 -> Max(3)) + Map(2 -> Max(4))
...