I am teaching Scala for the first time and my students are finding the deliberate "punning" involved in companion objects very confusing. Consider the following example:
class Stack { ... methods such as push/pop } object Stack { ... factory method(s) and possibly others }
The confusion comes in when I use verbal phrases such as "a stack object" or "stack objects" or especially "the stack object". My students have difficulty understanding whether I mean the singleton object Stack or objects of the Stack class.
I'm looking for alternative ways to verbalize such things that beginners can understand more easily. I've considered always referring to objects of the Stack class as "Stack instances" or "instances of Stack", but it seems crazy when trying to teach OO not to be able to call these things objects. I've been trying to ALWAYS use the phrase "singleton object" or "companion object" when talking about the singleton object Stack, but the syntax of Scala is working against me there because it only uses the word "object".
In this case, I could rename the singleton object StackFactory instead of Stack, but that's only an option for my own classes, not for the thousand and one companion objects already built into Scala.
Edit:
Sorry, I wasn't clear enough in my question. The main confusion happens NOT when referring to the companion object. In that case, as several people have noted, it is easy enough to use to a phrase such as "the companion object". Instead, the main confusion happens when referring to ordinary instances. Then, if I say "a stack object" (meaning some stack instance) or "the stack object" (meaning this particular instance), some fraction of the students will think I mean the companion object -- even though I did not use the word companion or singleton.
And I can see perfectly well where the confusion comes from, because the word "object" appears in the program text only with the companion object.
Object declarations are very useful for implementing the Singleton pattern. And the getInstance method can then be invoked like this. A companion object is a specific type of object declaration that allows an object to act similar to static objects in other languages (such as Java).
A companion object is an object that's declared in the same file as a class , and has the same name as the class. A companion object and its class can access each other's private members. A companion object's apply method lets you create new instances of a class without using the new keyword.
Companion objects are useful for storing state and methods that are common to all instances of a class but they do not use static methods or fields. They use regular virtual methods which can be overridden through inheritance. Scala truly has nothing static.
Solution: companion objectThe ability to extend interfaces and classes is one of the features that sets the companion objects apart from Java's static functionality. Also, companions are objects, we can pass them around to the functions and assign them to variables just like all the other objects in Kotlin.
I think Stack singleton
(if it stands alone) or Stack companion
(if it comes with a class) is the best way to name object Stack
. The Scala Language Reference calls them modules. But modules have by now been too much associated with run-time entities (as in OSGI) to be comfortable with that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With