Understanding Metaclasses

Damien Cassou, St├ęphane Ducasse and Luc Fabresse http://stephane.ducasse.free.fr

What You Will Learn

The Key: Only One Method Lookup

When a message is sent to an object, a method is searched starting from the class of the object and following the inheritance chain

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Every Object is an Instance of a Class

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Every Class Eventually Inherits From Object

Responsibility of Object

Class Object represents the common object behavior

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Every class is an instance of a metaclass

Classes are objects too!

Metaclass are Implicit

Metaclasses are automatically created when creating a class

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Metaclass Hierarchy Parallels Class Hierarchy

Sending a Message to a Class

Questions

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Every Metaclass Inherits from Class up to Behavior

Where new is Defined?

Responsibilities of Behavior

Behavior

Responsibilities of ClassDescription

ClassDescription

Responsibilities of Class

Class

Classes are Instances of Metaclasses

Metaclass and class inheritances are parallel

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

Every Metaclass is an Instance of Metaclass

Metaclass inherits from ClassDescription

Metaclass Responsibilities

Metaclass is responsible for creating and initializing a metaclass's sole instance (a Class)

Metaclasses in 7 Points

  1. Every object is an instance of a class
  2. Every class eventually inherits from Object
  3. Every class is an instance of a metaclass
  4. The metaclass hierarchy parallels the class hierarchy
  5. Every metaclass inherits from Class up to Behavior
  6. Every metaclass is an instance of Metaclass
  7. The metaclass of Metaclass is an instance of Metaclass

The metaclass of Metaclass is an Instance of Metaclass

Observations

  • When programming we do not really care about that!
  • Now the full graph is consistent
    • Any class can receive a message
    • Only one message lookup

A Consistent World

What You Should Know

/