如何将包导入 Scala REPL? [英] How to import package into Scala REPL?

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

问题描述

如何将包导入 Scala 的 REPL?我正在尝试导入这个名为 funsets 的包,它有一个名为FunSets"的对象.我尝试了 import funsets._import funsets._; 等的几种变体,但它仍然没有导入包中的函数和对象.

How do I import a package into Scala's REPL? I am trying to import this package called funsets which has an object named "FunSets". I tried several variations of import funsets._ and import funsets._; etc but it is still not importing the functions and object in the package.

推荐答案

一种方法是编译scala 类"并将它们放入 classpath.

例子,

1) 假设您有一个类 funsets.FunSets.scala

package funsets                                                                                        

object FunSets {                                                                                       

  def fun = "very fun"                                                                                 

}      

2) 首先使用 scalac 编译类.(如果你使用 sbt 那么 sbt compile 将把target/ 文件夹中的编译类)

2) Compile the class first using scalac. (If you use sbt then sbt compile would put compiled classes in target/ folder)

scalac FunSets.scala

您将看到创建的 funsets 文件夹/包,

You will see the funsets folder/package created,

$ ls -l
total 16
-rw-r--r--  1 updupd  NA\Domain Users   63 Dec 18 11:05 FunSets.scala
drwxr-xr-x  4 updupd  NA\Domain Users  136 Dec 18 11:06 funsets

3) 然后用类路径中的 funsets 包启动 REPL

3) Then start REPL with funsets package in classpath

$ scala -classpath .
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_151).
Type in expressions for evaluation. Or try :help.

scala> import funsets._
import funsets._

注意:如果使用sbt compile,请将target/classes放在classpath中.

Note: if you use sbt compile, put target/classes in classpath.

访问 Funsets 单例,

Access Funsets singleton,

scala> FunSets.fun
res0: String = very fun

另请阅读Scala REPL 无法导入包

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

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