I'm designing an object model for a fairly traditional object-oriented language.
I'm planning to implement Ruby-style lookup rules for object members: o.m looks for m in the class of o and its superclasses. This avoids the complexity of Python-style lookup rules, where the object o itself is considered first.
One consequence of these lookup rules is that, in order to support static members accessible on a class C as C.m, there needs to be a hierarchy of metaclasses as well as "normal" classes.
Here's a diagram of my latest design, showing the fundamental class relationships (first three rows) and how user-defined classes (Cookie and FileSystem) attach to them. I'm using the notation [[C]] to represent the metaclass of C, the solid arrow to denote inheritance ([subclass] ---|> [superclass]), and (mis)using the dotted arrow to denote instance-of ([object] - - > [class]):
https://i.postimg.cc/J0BrRRM6/Object-Model.jpg
Working left-to-right through the three columns, the following are true:
The first column contains objects (in pink), which hold instance variables:
- Every object is an instance of exactly one class (from every box there is one dashed arrow)
- Every object is an (indirect) instance of
Object (following one dashed arrow, then zero-or-more solid arrows, always leads to Object)
The middle column contains classes, which additionally hold methods that can be called on their instances:
The rightmost column contains metaclasses:
I spent quite a lot of time iterating on this diagram, eventually ending up with a design similar to Smalltalk's. One difference is that I have made Metaclass a direct subclass of Class instead of a sibling class. This makes metaclasses a subset of classes rather than having two disjoint sets - I think that's more intuitive, and it's also necessary for some of the above to hold.
On the other hand, the following also seem intuitive, but are not entirely true in this model:
A metaclass is a class whose instances are classes
- This is the truth, but not the whole truth:
Metaclass is also a class whose instances are classes, but it is not a metaclass (in particular, it's not a Singleton)
Similarly, every class is an instance of a metaclass
- This holds for "normal" classes (in the second column), but not metaclasses themselves
If A is a subclass of B, then [[A]] is a subclass of [[B]]
- This is true in Ruby ("The superclass of the metaclass is the metaclass of the superclass")
- Again, it's true here for the second column but not the third
- If we change the claim to "...then
[[A]] is a subclass of [[B]] or is [[B]] itself", then it's true for most metaclasses but still false for [[Object]]
Despite this, I haven't spotted any major contradictions in this model (unlike some earlier iterations where, for example, Metaclass accidentally derived from a class marked <<Singleton>> yet had multiple instances, or there were corner cases where C derived from <<Singleton>> but [[C]] didn't derive from <<[[Singleton]]>>). Can you see any issues with the model, or do you have any recommended improvements?
Or, is there anything you would add? For example, I can imagine a need for further modifiers similar to Singleton - e.g. Abstract (enforce zero instances) or Sealed (enforce zero subclasses). It might be appropriate to apply these to Class and Metaclass respectively.
Having said that, personally I'd prefer to simplify the model if possible - but I think most of its complexity is unavoidable. I also believe the diagram is still more coherent than the flowcharts explaining o.m in Python - and I'd rather have complex metaclass architecture (mostly hidden from users of the language) than complex lookup rules (potentially affecting programmers every time they call a method).