如何在 Scala REPL 中使用第三方库? [英] How to use third party libraries with Scala REPL?

查看:46
本文介绍了如何在 Scala REPL 中使用第三方库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've downloaded Algebird and I want to try out few things in the Scala interpreter using this library. How do I achieve this?

推荐答案

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

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

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

A more flexible approach is to use sbt to 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.

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

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.

这是使用 algebird 的 build.sbt:

Here is a build.sbt for using algebird:

name := "Scala Playground"

version := "1.0"

scalaVersion := "2.10.2"

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

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

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:

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

这篇关于如何在 Scala REPL 中使用第三方库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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