它是一种具有特征和扩展该特征的同名对象的好样式吗? [英] Is it a good style having a trait and an equally named object extending that trait?

查看:29
本文介绍了它是一种具有特征和扩展该特征的同名对象的好样式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我浏览 Paul Phillips GitHub 存储库时,我注意到他经常使用某种结构:

While I was browsing Paul Phillips GitHub repositories I noticed that he often uses a certain structur:

trait A {
 // ...
}
object A extends A

例如这里:scala-improving,字符串首先,来自 Java,我不知道特征和对象在同一范围内具有相同的名称.
现在我问它有什么用?与使用所有 trait 成员直接定义对象相比,有什么优势?好的,我知道 trait 可以混入,但我假设在实践中只使用对象.

For example here: scala-improving, strings First, comming from Java, I wasn´t aware of having the same name for a trait and an object in the same scope.
And now I am asking what is it good for? What are the advantages over directly defining the object with all the trait members? Ok, I know that the trait can be mixed in, but I assume using only the object in practice.

推荐答案

这种模式至少派上用场的一种情况是在构建函数库时.您可以将函数(实际上是方法,但让我们在此上下文中将它们称为函数)重新组合为几个特征(然后可以将其视为模块),例如 MyLibAMyLibBMyLibC 等.然后,如果您为它们中的每一个定义了一个实现它的对象,则您的库的用户可以通过编写以下内容轻松使用它,例如:

At least one case where this pattern comes in handy is when you're building a library of functions. You can regroup the functions (actually methods, but let's call them functions in this context) into several traits (which can then be seen as modules), say, MyLibA, MyLibB, MyLibC, etc. Then, if for each of them, you define an object implementing it, users of your lib can easily use it by writing, for instance, this:

import mypackage.MyLibA._

(假设 mypackage 是包含包).此外,您可以通过提供一个对象 MyLib 来轻松地提供一种导入 lib 的所有功能的方法,如下所示:

(assuming mypackage is the containing package). Moreover, you can easily provide a way to import all functions of your lib by providing an object MyLib as follows:

object MyLib extends MyLibA with MyLibB with MyLibC

然后用户可以简单地导入 mypackage.MyLib._ 而不是单独为每个模块编写导入.更好的是,您可以定义一个 package 对象 mypackage extends MyLibA with MyLibB with MyLibC,然后用户只需编写 import mypackage._.而且,好处是那些只想在一行中只导入 MyLibAMyLibB 的挑剔用户也可以自由定义他们自己的导入对象",甚至可能完成它使用它们自己的实用程序函数,或覆盖您自己的函数:

and then users can simply import mypackage.MyLib._ instead of writing an import for each module separately. Better yet, you could define a package object mypackage extends MyLibA with MyLibB with MyLibC, and then users would simply write import mypackage._. And, the bonus is that picky users who would like to import only MyLibA and MyLibB in one line are also free to define their own "import object," possibly even completing it with their own utility functions, or overriding your own:

object UserLibImport extends MyLibA with MyLibB {
    def userAdditionalFunction = /* ... */
    override def someRedefinedLibAFunction = /* ... */
}

... 然后使用 import UserLibImport._

所以,我想说,在这种情况下,它不仅风格好,而且强烈建议以这种方式提供您的功能,因为它允许最大的灵活性.

So, I'd say, in this case, not only is it good style, it's highly recommended to provide your functions in this way, as it allows for maximal flexibility.

(这也在 这个答案.)

这篇关于它是一种具有特征和扩展该特征的同名对象的好样式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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