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

查看:161
本文介绍了如何在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控制台.它将下载您的所有依赖关系,并在类路径上自动启动具有所有依赖关系的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天全站免登陆