CS 6140: Smalltalk and The Truth About Metaclasses
This summary comes from:
Creating new Smalltalk classes
Smalltalk metaclasses
Object subclass: #Account
instanceVariableNames: 'balance'
classVariableNames: ''
poolDictionaries: ''
category: nil !
!Account class methodsFor: 'instance creation'!
new
| r |
r := super new.
r init.
^r
! !
!Account methodsFor: 'instance initialization'!
init
balance := 0
! !
- 1. Account inherits from Object, and calls Object's new.
- 2. This returns an instance of type Account, with storage for balance.
- 3. But Object doesn't really have a new.
- 4. The new method is implemented inside of a Behavior class, a subclass of
Object, a subclass of the abstract class Class, a subclass of abstract
ClassDescription, a subclass of concrete Behavior, a subclass of Object, ...
- 5. Behavior instances are light-weight classes, they do not have separate
metaclasses, instead share Behavior as their metaclass.
- 6. The Class class is the abstract superclass of all metaclasses.
- 7. Object is the superclass of all instances as well as all metaclasses.
- 8. Metaclass is an instance of an instance of itself.
- 9. Metaclass is a subclass of ClassDescription.
- 10. Class methods are a lie, they're simply instance methods that are understood
by instances of metaclasses.
- 11. Parallel hierarchy between classes and metaclasses.
- 12. Classes are objects.
- 13. When you create a class, Smalltalk creates a metaclass.
- 14. A class describes how methods for its instances work.
- 15. A metaclass describes how class methods for that same class work.
- 16. Metaclass implements creation and initialization of its instances, which are
classes (subclasses of Class).
- 17. Implements a clean, elegant and (with some contemplation) understandable
facility for self-definition of classes. Allows classes to talk about
themselves, posing the foundation for the creation of browsers.
- 18. Java has reflection.