封装对象
什么是包对象,与其说是概念,不如说是它们的用法?
我试图让一个示例工作,我工作的唯一形式如下:
package object investigations {
val PackageObjectVal = "A package object val"
}
package investigations {
object PackageObjectTest {
def main(args: Array[String]) {
println("Referencing a package object val: " + PackageObjectVal)
}
}
}
到目前为止我所做的观察是:
package object _root_ { ... }
是不允许的(这是合理的),
package object x.y { ... }
也是不允许的。
看来包对象必须在直接父包中声明,如果按照上面的方式编写,则需要大括号分隔的包声明形式。
它们常用吗?如果是这样,怎么办?
What are package objects, not so much the concept but their usage?
I've tried to get an example working and the only form I got to work was as follows:
package object investigations {
val PackageObjectVal = "A package object val"
}
package investigations {
object PackageObjectTest {
def main(args: Array[String]) {
println("Referencing a package object val: " + PackageObjectVal)
}
}
}
Observations I've made so far are:
package object _root_ { ... }
is disallowed (which is reasonable),
package object x.y { ... }
is also disallowed.
It seems that a package object must be declared in the immediate parent package and, if written as above, the brace delimited package declaration form is required.
Are they in common use? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,您会将包对象放在与其对应的包中名为
package.scala
的单独文件中。您还可以使用嵌套包语法,但这很不寻常。包对象的主要用例是当您使用包定义的 API 时,需要在包内部以及包外部的各个位置进行定义。下面是一个示例:
现在,该包对象内的定义在整个包
foo.bar
中都可用。此外,当该包之外的人导入foo.bar._
时,定义也会被导入。通过这种方式,您可以防止要求 API 客户端发出额外的导入来有效地使用您的库 - 例如,在 scala-swing 中,您需要编写
才能拥有
onEDT
等所有优点以及来自的隐式转换Tuple2
到Dimension
。Normally you would put your package object in a separate file called
package.scala
in the package that it corresponds to. You can also use the nested package syntax but that is quite unusual.The main use case for package objects is when you need definitions in various places inside your package as well as outside the package when you use the API defined by the package. Here is an example:
Now the definitions inside that package object are available inside the whole package
foo.bar
. Furthermore the definitions get imported when someone outside of that package importsfoo.bar._
.This way you can prevent to require the API client to issue additional imports to use your library effectively - e.g. in scala-swing you need to write
to have all the goodness like
onEDT
and implicit conversions fromTuple2
toDimension
.虽然莫里茨的答案是正确的,但需要注意的另一件事是包对象是对象。除此之外,这意味着您可以使用混合继承从特征构建它们。 Moritz 的示例可以写为
此处版本控制是一个抽象特征,它表示包对象必须具有“版本”方法,而 JodaAliases 和 JavaAliases 是包含方便的类型别名的具体特征。所有这些特征都可以被许多不同的包对象重用。
While Moritz's answer is spot on, one additional thing to note is that package objects are objects. Among other things, this means you can build them up from traits, using mix-in inheritance. Moritz's example could be written as
Here Versioning is an abstract trait, which says that the package object must have a "version" method, while JodaAliases and JavaAliases are concrete traits containing handy type aliases. All of these traits can be reused by many different package objects.
你可能会做比直接找到源头更糟糕的事情。 :)
https://lampsvn.epfl .ch/trac/scala/browser/scala/trunk/src/library/scala/package.scala
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/collection/immutable /package.scala
You could do worse than to go straight to the source. :)
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/package.scala
https://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scala/collection/immutable/package.scala
但 Scala 3 则不然,计划于 2020 年中期发布,基于 多蒂,如在这里:
Not so with Scala 3, scheduled to be released mid-2020, based on Dotty, as in here: