Gregory Kornblum
Gregory Kornblum

Reputation: 175

Smalltalk tricky syntax

In an exam example they ask if the statement is valid, and if it is, what it returns:

Object class superclass class class

I can't follow that many messages by my own. Please, help!

Upvotes: 2

Views: 137

Answers (2)

Leandro Caniglia
Leandro Caniglia

Reputation: 14858

One interesting aspect of the expression

Object class

is that its answer is not a Class but a Metaclass.

The reason why Object class is not a Class is that Metaclass is not a subclass of Class. More importantly, Object class does not inherit the behavior defined in Class.

Of course, one could be tempted to say that under the same criterion Object would not be a Class either because it is not an instance of Class but of its metaclass (namely, Object class.) But this is just because of some technicalities (or implementation details, if you will.) The truth is that Object (and all other classes for this matter) inherit behavior from Class, and that's why we say that they are classes, they behave as instances of Class (which, interesting enough, has no instances at all.)

The way to achieve this property consists in enforcing the following equation

ProtoObject class superclass == Class

Thanks to this relationship the class side of ProtoObject inherits the (instance) behavior of Class (even though Class has no subclasses.)

Now, why then the expression

Object class superclass

makes sense? If the receiver of superclass is not even a class, should it understand the message superclass?

The answer is yes, not only classes but metaclasses have a superclass. And here, again, the reason has to do with the inherited behavior.

Every programmer expects that if class A inherits from B, then A class will inherit all the methods of the class side of B. In other words,

A class superclass == B class

or, equivalently,

A class superclass == A superclass class

Note however that this relationship doesn't hold for A == ProtoObject, as we saw above (ProtoObject class superclass == Class.)

So, in the presence of ProtoObject, the expression

Object class superclass class class

becomes

Object superclass class class class

or

ProtoObject class class class

or

Metaclass class

because ProtoObject class is an instance of Metaclass and so its class is Metaclass.

To complete the answer let's calculate

ProtoObject class superclass class class

In this case we get

Class class class

or

Metaclass

Upvotes: 0

Oak
Oak

Reputation: 26868

Assuming Object is the root of the type system, you just need to carefully follow the rules:

  1. Every normal class X is an instance of its metaclass X class
  2. Every metaclass X class is an instance of the normal class Metaclass.
  3. The metaclass inheritance graph follows the regular object inheritance graph, except that while the root object inherits from nil, its metaclass inherits from Class.

So it would be:

|             Expression              |     Type     |
|-------------------------------------|--------------|
| Object                              | Object       |
| Object class                        | Object class |
| Object class superclass             | Class        |
| Object class superclass class       | Class class  |
| Object class superclass class class | Metaclass    |

In practice Object isn't the root, its parent ProtoObject is; so if the exam authors took that into consideration, the above would then end with Metaclass class.

Upvotes: 3

Related Questions