Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to talk about companion objects vs regular objects?

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.

like image 659
Chris Okasaki Avatar asked Sep 19 '12 14:09

Chris Okasaki


People also ask

What is the difference between object and 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).

What is the point of a companion object?

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.

What is the advantage of companion objects in Scala?

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.

Why do we use companion object as a kind of replacement for Java static fields in Kotlin?

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.


1 Answers

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.

like image 105
Martin Odersky Avatar answered Sep 27 '22 23:09

Martin Odersky