Scala:将包导入包对象 [英] Scala: Importing packages into package objects

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

问题描述

我无法将软件包导入到软件包对象中.它似乎在Eclipse中不起作用,因此我改用了intellij.一方面,该功能似乎正在运行,所以我为大多数软件包创建了软件包对象.现在它似乎根本不起作用.这是文件package.scala中的一个包对象,该包文件本身可以很好地编译:

I'm having trouble importing packages into package objects. It didn't seem to work in Eclipse and so I switched to intellij. At one point the feature seemed to be working and so I created package objects for most packages. Now it doesn't seem to be working at all. Here's a package object in file package.scala, the package file itself compiles fine:

package rStrat.rSwing
package testSw //Edited for clarity

object testSw
{
  import rStrat._
  import rSwing.topUI._
}

这是来自相同模块和包的类文件.

and here's a class file from the same module and package.

package rStrat.rSwing.testSw

object MainTest {
  def main(args: Array[String])
  {
    val testApp = new AppWindow //Appwindow is a member of topUI
    testApp.open
  }
}

如果我将topUI软件包直接导入MainTest文件,则可以正常工作.无论我尝试导入整个程序包还是特定类,都没有什么区别.这是法律标文吗?IDE有问题吗?

It works fine if I import the topUI package straight into the MainTest file. it makes no difference whether I try and import the whole package or a particular class. is this legal scala? Is the problem with the IDEs?

我正在使用Scala 2.92 Final,Intellij 11.1.1,JDK 1.6.0_31,Eclipse 3.7.2

I'm using Scala 2.92 Final, Intellij 11.1.1, JDK 1.6.0_31, Eclipse 3.7.2

推荐答案

Scala没有一流的进口.如果包只能包含类和特征的声明,则包对象可以包含任何其他有效的Scala声明,例如 var val def type (类型别名),隐式内容.尽管可以在任何对象中导入东西,但它们不会传递到包的其余部分,因此对于世界其他地方是不可见的.

Scala does not have first class imports. Where a package can only contain declarations of classes and traits, a package object can contain any other valid Scala declaration like var, val, def, type (type alias), implicit stuff. And although as in any object you can import things, they don't transitively propagate to the rest of the package and are thus not visible to the rest of the world.

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

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