能够在scala解释器中导入对象,但不能在文件中导入

时间:2015-06-27 03:29:22

标签: scala import interpreter

我正在做一系列问题并且认为我会将公共代码压缩到一个我可以导入的实用程序文件中,而不是每次都复制粘贴函数。所以我创建了一个文件u.scala:

object u
{

val test = 10

def primeFacs(num : Int) : List[Int] = 
{
    ...
}


def allMultCombos(l : List[Int]) : List[Int] = 
{
    ...
}


def allFacs(num : Int) : List[Int] = 
{
    ...
}

def properFacs(num : Int) : List[Int] =
{
    ...
}

}

并使用scalac u.scala进行编译。然后,在同一目录中的文件中,我尝试了import u以及import u._,但都没有导入它。但是,当我打开scala解释器时,它导入它就好了:

r@ANA:~/Documents/prog/euler_scala$ ls
1.scala     12.scala    15.scala    18.scala    20.scala    23.scala    5.scala     7.scala     9.scala
10.scala    13.scala    16.scala    19.scala    21.scala    3.scala     6.scala     8.py        test.scala
11.scala    14.scala    17.scala    2.scala     22.scala    4.scala     67.scala    8.scala     u.scala

r@ANA:~/Documents/prog/euler_scala$ scalac u.scala
r@ANA:~/Documents/prog/euler_scala$ scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import u
     | ._
import u._

scala> test
res0: Int = 10

scala> primeFacs(10)
res1: List[Int] = List(5, 2)

scala> :quit
r@ANA:~/Documents/prog/euler_scala$ scala 23.scala
/Users/r/Documents/prog/euler_scala/23.scala:1: error: not found: value u
import u._
       ^
/Users/r/Documents/prog/euler_scala/23.scala:10: error: not found: value allFacs
    val f = allFacs(i)
                ^
two errors found
r@ANA:~/Documents/prog/euler_scala$ ls
1.scala                        17.scala                       3.scala                        9.scala
10.scala                       18.scala                       4.scala                        test.scala
11.scala                       19.scala                       5.scala                        u$$anonfun$1.class
12.scala                       2.scala                        6.scala                        u$$anonfun$properFacs$1.class
13.scala                       20.scala                       67.scala                       u$.class
14.scala                       21.scala                       7.scala                        u.class
15.scala                       22.scala                       8.py                           u.scala
16.scala                       23.scala                       8.scala
r@ANA:~/Documents/prog/euler_scala$ 

所有内容都是从同一目录运行的。是什么给了什么?

0 个答案:

没有答案