Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: multiple objects and classes in a single file or each object/class its own file

Tags:

scala

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.

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?

like image 513
Lucas Kauffman Avatar asked Jun 23 '14 12:06

Lucas Kauffman


People also ask

Can a Scala file have multiple classes?

In Scala, it is perfectly valid to have multiple classes within a single file AS LONG AS they are tightly related.

Can there be multiple objects of one class?

Multiple objects, or instances of a class can be created in a single HLU program, just as you declare multiple variables of the same type in any program. For example, the TextItem class is a template for creating an object that contains a text string.

Can I have a class and object with same name in a Scala file?

An object with the same name as a class is called a companion object. Conversely, the class is the object's companion class. A companion class or object can access the private members of its companion. Use a companion object for methods and values which are not specific to instances of the companion class.

What is the difference between object and class in Scala?

Difference Between Scala Classes and Objects Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.

What is a class and object in Scala?

Class and Object in Scala. Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real-life entities. A class is a user-defined blueprint or prototype from which objects are created. Or in other words, a class combines the fields and methods(member function which defines actions) into a single unit.

What is a singleton class in Scala?

A singleton is a class that can have only one instance, i.e., Object. You create singleton using the keyword object instead of class keyword. Since you can't instantiate a singleton object, you can't pass parameters to the primary constructor. You already have seen all the examples using singleton objects where you called Scala's main method.

Why is Scala more object-oriented than Java?

Scala is more object-oriented than Java because in Scala, we cannot have static members. Instead, Scala has singleton objects. A singleton is a class that can have only one instance, i.e., Object. You create singleton using the keyword object instead of class keyword.

What is the default modifier of class in Scala?

Note: The default modifier of the class is public. It is a basic unit of Object Oriented Programming and represents the real-life entities. A typical Scala program creates many objects, which as you know, interact by invoking methods. An object consists of : State: It is represented by attributes of an object.


1 Answers

There is a chapter in the official Scala Style Guide on this. It's pretty clear in itself, but I'll just leave some quotes here.

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 sealed only works within the same file.

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 must be 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:

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.

like image 157
DCKing Avatar answered Oct 06 '22 08:10

DCKing