Scala:单个文件中的多个对象和类或每个对象/类都有自己的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24365548/
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
Scala: multiple objects and classes in a single file or each object/class its own file
提问by Lucas Kauffman
I've recently started programming in Scala, coming from Python and Java I was wondering what the correct way or the accepted way is when defining objects/classes in Scala. Scala supports, just like python, to add several class or object definitions in a single file.
我最近开始在 Scala 中编程,来自 Python 和 Java 我想知道在 Scala 中定义对象/类时正确的方法或公认的方法是什么。Scala 支持,就像 Python 一样,在单个文件中添加多个类或对象定义。
So purely from an accepted structure perspective, does every object need to be defined in its own file or are you allowed to choose this yourself?
因此,纯粹从公认的结构角度来看,是否每个对象都需要在自己的文件中定义,还是允许您自己选择?
回答by DCKing
There is a chapter in the official Scala Style Guideon this. It's pretty clear in itself, but I'll just leave some quotes here.
官方Scala 风格指南中有一个章节介绍了这一点。这本身就很清楚,但我会在这里留下一些引述。
The core idea is:
核心思想是:
As a rule, files should contain a single logical compilation unit. By “logical” I mean a class, trait or object.
通常,文件应包含单个逻辑编译单元。“逻辑”是指一个类、特征或对象。
There is, of course, an exception for companion objects:
当然,伴生对象有一个例外:
One exception to this guideline is for classes or traits which have companion objects. Companion objects should be grouped with their corresponding class or trait in the same file.
该指南的一个例外是具有伴生对象的类或特征。伴生对象应与其对应的类或特征分组在同一文件中。
There is also the fact that sealedonly works within the same file.
还有一个事实,即sealed只能在同一个文件中工作。
Despite what was said above, there are some important situations which warrant the inclusion of multiple compilation units within a single file. One common example is that of a sealed trait and several sub-classes. Because of the nature of sealed superclasses (and traits), all subtypes mustbe included in the same file.
尽管上面说了,有一些重要的情况需要在单个文件中包含多个编译单元。一个常见的例子是一个密封特性和几个子类。由于密封超类(和特征)的性质,所有子类型必须包含在同一个文件中。
Most of the time, case classes are just simple data containers and can be grouped together.
大多数情况下,案例类只是简单的数据容器,可以组合在一起。
Another case is when multiple classes logically form a single, cohesive group, sharing concepts to the point where maintenance is greatly served by containing them within a single file.
另一种情况是当多个类在逻辑上形成一个单一的、有凝聚力的组时,共享概念到通过将它们包含在单个文件中来极大地维护维护的程度。
Finally, there is a naming convention for exempted multi-unit Scala files:
最后,豁免的多单元 Scala 文件有一个命名约定:
All multi-unit files should be given camelCase names with a lower-case first letter.
所有多单元文件都应该以驼峰命名,首字母小写。
So: put your Scala classes and objects in separate files, unless they fall into one of the three mentioned exceptions.
所以:将您的 Scala 类和对象放在单独的文件中,除非它们属于上述三个例外之一。
回答by Jordan Parmer
In Scala, it is perfectly valid to have multiple classes within a single file AS LONG ASthey are tightly related.
在Scala中,它是完全有效的单个文件中有多个类,只要他们是紧密相关的。
But not all languages encourage this convention, and I think it is worth considering the reason.
但并非所有语言都鼓励这种约定,我认为这是值得考虑的原因。
I personally dislike it when people put multiple classes into a single file because it makes it harder to find a class definition. This is magnified in code reviews where I want to be able to review code as quickly as possible without digging around.
当人们将多个类放入单个文件时,我个人不喜欢它,因为它使查找类定义变得更加困难。这在代码中被放大了,我希望能够尽快代码而无需四处寻找。
Cons
缺点
- Code reviews require me to do more searching to find a class
- I don't like having to
grepto find a file - A consistent naming convention allows me to use my text editor or IDE tools to quickly open a file by the class name
- 代码需要我做更多的搜索才能找到一个类
- 我不喜欢必须
grep找到一个文件 - 一致的命名约定允许我使用我的文本编辑器或 IDE 工具按类名快速打开文件
Pros
优点
- As Jesper pointed out, certain scenarios require it
- Support classes/traits are kept hidden to minimize file structure "noise"
- 正如 Jesper 指出的那样,某些场景需要它
- 支持类/特征保持隐藏以最小化文件结构“噪音”
回答by Jesper
Sometimes you have to put several traits, classes or objects in one source file, particularly when you are using sealedtraits. A sealedtrait can only be extended inside the same source file.
有时您必须将多个特征、类或对象放在一个源文件中,尤其是在使用sealed特征时。甲sealed性状可以只同一个源文件内进行扩展。

