scala 包对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3400734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Package objects
提问by Don Mackenzie
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?
它们是否通用?如果是这样,如何?
回答by Moritz
Normally you would put your package object in a separate file called package.scalain the package that it corresponds to. You can also use the nested package syntax but that is quite unusual.
通常,您会将包对象放在一个单独的文件package.scala中,该文件在它对应的包中调用。您也可以使用嵌套包语法,但这很不寻常。
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:
包对象的主要用例是,当您使用包定义的 API 时,您需要在包内以及包外的各个位置进行定义。下面是一个例子:
// file: foo/bar/package.scala
package foo
package object bar {
// package wide constants:
def BarVersionString = "1.0"
// or type aliases
type StringMap[+T] = Map[String,T]
// can be used to emulate a package wide import
// especially useful when wrapping a Java API
type DateTime = org.joda.time.DateTime
type JList[T] = java.util.List[T]
// Define implicits needed to effectively use your API:
implicit def a2b(a: A): B = // ...
}
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 imports foo.bar._.
现在该包对象内的定义在整个包内可用foo.bar。此外,当该包之外的人导入时,定义会被导入foo.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
通过这种方式,您可以防止要求 API 客户端发出额外的导入以有效地使用您的库 - 例如在 scala-swing 中,您需要编写
import swing._
import Swing._
to have all the goodness like onEDTand implicit conversions from Tuple2to Dimension.
拥有onEDT从Tuple2to 的所有优点和隐式转换Dimension。
回答by Dave Griffith
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
虽然 Moritz 的回答是正确的,但要注意的另一件事是包对象是对象。除此之外,这意味着您可以使用混合继承从特征构建它们。莫里茨的例子可以写成
package object bar extends Versioning
with JodaAliases
with JavaAliases {
// package wide constants:
override val version = "1.0"
// or type aliases
type StringMap[+T] = Map[String,T]
// Define implicits needed to effectively use your API:
implicit def a2b(a: A): B = // ...
}
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.
这里的 Versioning 是一个抽象特征,它表示包对象必须有一个“版本”方法,而 JodaAliases 和 JavaAliases 是包含方便的类型别名的具体特征。所有这些特征都可以被许多不同的包对象重用。
回答by Alex Cruise
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/package.scala
回答by VonC
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.
包对象的主要用例是,当您使用包定义的 API 时,您需要在包内以及包外的各个位置进行定义。
Not so with Scala 3, scheduled to be released mid-2020, based on Dotty, as in here:
并非如此,斯卡拉3,按计划将发布中期到2020年,基于斑点狗,因为在这里:
Toplevel Definitions
All kinds of definitions can be written on the toplevel.
Package objects are no longer needed, will be phased out.
顶级定义
各种定义都可以写在顶层。
不再需要包对象,将被逐步淘汰。
package p
type Labelled[T] = (String, T)
val a: Labelled[Int] = ("count", 1)
def b = a._2
def hello(name: String) = println(i"hello, $name)

