Classes and Metaclasses an Analysis Stphane Ducasse Stephane

  • Slides: 29
Download presentation
Classes and Metaclasses - an Analysis Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic.

Classes and Metaclasses - an Analysis Stéphane Ducasse Stephane. Ducasse@univ-savoie. fr http: //www. listic. univ-savoie. fr/~ducasse/ S. Ducasse 1

License: CC-Attribution-Share. Alike 2. 0 http: //creativecommons. org/licenses/by-sa/2. 0/ S. Ducasse 2

License: CC-Attribution-Share. Alike 2. 0 http: //creativecommons. org/licenses/by-sa/2. 0/ S. Ducasse 2

Goals “Some books are to be tasted, others to be swallowed, and some few

Goals “Some books are to be tasted, others to be swallowed, and some few to be chewed and digested” — Francis Bacon, Of Studies • Recap on Instantiation • Recap on Inheritance S. Ducasse 3

Warning At first sight, a difficult topic! You can live without really understanding them,

Warning At first sight, a difficult topic! You can live without really understanding them, but metaclasses provide a uniform model, and you will make less errors if you learn how they work, and you will really understand the object model S. Ducasse 4

The Meaning of “Instance of” • • • S. Ducasse Every object is an

The Meaning of “Instance of” • • • S. Ducasse Every object is an instance of a class. Every class (except Object) is ultimately a subclass of Object. When an. Object receives a message, the method is looked up in its class and/or its superclasses. A class defines the structure and the behavior of all its instances. Each instance possesses its own set of values. Each instance shares its behavior with other instances. This behavior is defined in its class, and is accessed via the instance of link. 5

Metaclass • • S. Ducasse Everything is an object Every object is instance of

Metaclass • • S. Ducasse Everything is an object Every object is instance of exactly one class A class is also an object, and is an instance of its metaclass An object is a class if and only if it can create instances of itself. 6

Class Responsibilities • • • instance creation class information (inheritance link, instance variables, method

Class Responsibilities • • • instance creation class information (inheritance link, instance variables, method compilation. . . ) Examples: • • • S. Ducasse Node all. Subclasses -> Ordered. Collection (Work. Station Output. Server Workstation File) Lan. Printer all. Instances -> #() Node inst. Var. Names -> #('name' 'next. Node') Workstation with. Name: #mac -> a. Workstation selectors -> Identity. Set (#accept: #originate: ) Workstation can. Understand: #next. Node -> true 7

Metaclass by Example Node all. Subclasses -> Ordered. Collection (Work. Station Output. Server Workstation

Metaclass by Example Node all. Subclasses -> Ordered. Collection (Work. Station Output. Server Workstation File. Server Print. Server) Print. Server all. Instances -> () Node inst. Var. Names -> ('name' 'next. Node') Workstation with. Name: mac -> a. Workstation selectors -> Identity. Set (accept: originate: ) Workstation can. Understand: next. Node -> true S. Ducasse 8

The Meaning of Is-a • • Every object is an instance of a class.

The Meaning of Is-a • • Every object is an instance of a class. When an. Object receives a message, the method is looked up in its class And it continues possibly in its superclasses Every class is ultimately a subclass of Object (except Object). S. Ducasse 9

A Class is an Object too… So messages sent to a class are looked

A Class is an Object too… So messages sent to a class are looked up into the class of the class Node with. Name: #node 1 Node is an instance of “Node class” with. Name: is looked up in the class “Node class” with. Name: defined in “Node class” lookup stops + method executed S. Ducasse 10

Class Parallel Inheritance S. Ducasse 11

Class Parallel Inheritance S. Ducasse 11

Lookup and Class Methods S. Ducasse 12

Lookup and Class Methods S. Ducasse 12

Class Parallel inheritance • Workstation with. Name: #mac • • • S. Ducasse Workstation

Class Parallel inheritance • Workstation with. Name: #mac • • • S. Ducasse Workstation is an instance of Workstation class => with. Name: is looked up in the class Workstation class with. Name: is not defined in Workstation class => lookup continues in the superclass of Workstation class = Node class with. Name: is defined in Node class => lookup stops + method executed 13

Responsibilities of Object & Class Object represents the common behavior (like error, halting. .

Responsibilities of Object & Class Object represents the common behavior (like error, halting. . . ) shared by all the instances (final instances and classes) all the classes should inherit ultimately from Object -> Workstation inherits from Node -> Node inherits from Object Class represents the common behavior of all the classes (compilation, method storing, instance variable storing) Class inherits from Object because Class is an Object, although a special one -> Class knows how to create instances So all the classes should inherit ultimately from Class S. Ducasse 14

A Fragile Reflective Kernel The kernel of CLOS and Obj. Vlisp but not the

A Fragile Reflective Kernel The kernel of CLOS and Obj. Vlisp but not the kernel of Smalltalk S. Ducasse 15

Singleton with explicit metaclasses S. Ducasse 16

Singleton with explicit metaclasses S. Ducasse 16

Deeper into It S. Ducasse 17

Deeper into It S. Ducasse 17

Smalltalk Metaclasses in 7 points No explicit metaclasses, only implicit non-sharable metaclasses. (1) Every

Smalltalk Metaclasses in 7 points No explicit metaclasses, only implicit non-sharable metaclasses. (1) Every class is ultimately a subclass of Object (except Object itself) Object Behavior Class. Description Class Metaclass (II) Every object is an instance of a class = every class is an instance of a class which is its metaclass. S. Ducasse 18

Smalltalk Metaclasses in 7 points (3) Every class is an instance of a metaclass.

Smalltalk Metaclasses in 7 points (3) Every class is an instance of a metaclass. Every user defined class is the sole instance of another class (a metaclass). Metaclasses are system generated and they are unnamed. You can access them by sending the message class to a class. Point class name -> ‘Point class’ S. Ducasse 19

Smalltalk Metaclasses in 7 points If X is a subclass of Y then X

Smalltalk Metaclasses in 7 points If X is a subclass of Y then X class is a subclass of Y class. But what is the superclass of the metaclass of Object? The superclass of Object class is Class All metaclasses are (ultimately) subclasses of Class. But metaclasses are also objects so they should be instances of a Metaclass 20 S. Ducasse

Smalltalk Metaclasses in 7 points (5) Every metaclass is an instance of Metaclass. So

Smalltalk Metaclasses in 7 points (5) Every metaclass is an instance of Metaclass. So Metaclass is an instance of itself Object : common object behavior Class: common class behavior (name, multiple instances) Metaclass: common metaclass behavior (no name, unique instance) S. Ducasse 21

Smalltalk Metaclasses in 7 points (6) The methods of Class and its superclasses support

Smalltalk Metaclasses in 7 points (6) The methods of Class and its superclasses support the behavior common to those objects that are classes. (7) The methods of instances of Metaclass add the behavior specific to particular classes. Methods of instance of Metaclass = methods of “Packet class” = class methods (for example with. Name: An instance method defined in Behavior or Class. Description, is available as a class method. Example: new, new: S. Ducasse 22

Complete Picture S. Ducasse 23

Complete Picture S. Ducasse 23

Final Thoughts Finally it is not sure that the Smalltalk model is more complex

Final Thoughts Finally it is not sure that the Smalltalk model is more complex than the one of Obj. Vlisp. If we consider the programmer view of a class, Smalltalk is simpler If we consider the meta-programmer, Obj. Vlisp is simpler S. Ducasse 24

Responsibilities S. Ducasse 25

Responsibilities S. Ducasse 25

Behavior Responsibilities Minimum state necessary for objects that have instances. Basic interface to the

Behavior Responsibilities Minimum state necessary for objects that have instances. Basic interface to the compiler. State: class hierarchy link, method dictionary, description of instances (representation and number) Methods: creating a method dictionary, compiling method instance creation (new, basic. New, new: , basic. New: ) class into hierarchy ( superclass: , add. Subclass: accessing (selectors, all. Selectors, compiled. Method. At: ) accessing instances and variables (all. Instances, inst. Var. Names) accessing class hierarchy (superclass, subclasses) S. Ducasse testing (has. Methods, includes. Selector, 26

Class. Description Responsibilities Class. Description adds a number of facilities to basic Behavior: named

Class. Description Responsibilities Class. Description adds a number of facilities to basic Behavior: named instance variables category organization for methods the notion of a name (abstract) the maintenance of the Changes set, and logging changes most of the mechanisms needed for file. Out Class. Description is an abstract class: its facilities are intended for inheritance by the two subclasses, Class and Metaclass. S. Ducasse 27

Metaclass and Class Responsibilities Metaclass initialization of class variables creating initialized instances of the

Metaclass and Class Responsibilities Metaclass initialization of class variables creating initialized instances of the metaclass’s sole instance creation (subclass. Of: ) metaclass instance protocol (name: in. Environment: subclass. Of: . . ) Class adds naming for class Class adds the representation for class. Variable names and shared pool variables (add. Class. Vara. Names, add. Shared. Pool: , initialize) S. Ducasse 28

Summary Classes are objects too A class is the unique instance of another class,

Summary Classes are objects too A class is the unique instance of another class, its metaclass S. Ducasse 29