stoop 304-metaclasses

29
S.Ducasse 1 QuickTime™ TIFF (Uncompr are needed t Stéphane Ducasse [email protected] http://www.listic.univ-savoie.f r/~ducasse/ Classes and Metaclasses - an Analysis

Upload: the-world-of-smalltalk

Post on 09-May-2015

512 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Stoop 304-metaclasses

S.Ducasse 1

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Stéphane [email protected]://www.listic.univ-savoie.fr/~ducasse/

Classes and Metaclasses - an Analysis

Page 2: Stoop 304-metaclasses

S.Ducasse 2

License: CC-Attribution-ShareAlike 2.0http://creativecommons.org/licenses/by-sa/2.0/

Page 3: Stoop 304-metaclasses

S.Ducasse 3

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

Page 4: Stoop 304-metaclasses

S.Ducasse 4

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

Warning

Page 5: Stoop 304-metaclasses

S.Ducasse 5

•Every object is an instance of a class.•Every class (except Object) is ultimately a

subclass of Object.•When anObject 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.

The Meaning of “Instance of”

Page 6: Stoop 304-metaclasses

S.Ducasse 6

•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.

Metaclass

Page 7: Stoop 304-metaclasses

S.Ducasse 7

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

variables, method compilation...)• Examples:

• Node allSubclasses -> OrderedCollection (WorkStation OutputServer Workstation File)

• LanPrinter allInstances -> #()• Node instVarNames -> #('name' 'nextNode')• Workstation withName: #mac -> aWorkstation• Workstation selectors -> IdentitySet (#accept:

#originate:)• Workstation canUnderstand: #nextNode -> true

Page 8: Stoop 304-metaclasses

S.Ducasse 8

Node allSubclasses -> OrderedCollection (WorkStation OutputServer Workstation

FileServer PrintServer)PrintServer allInstances -> ()Node instVarNames -> ('name' 'nextNode')Workstation withName: mac -> aWorkstationWorkstation selectors -> IdentitySet (accept: originate:)Workstation canUnderstand: nextNode -> true

Metaclass by Example

Page 9: Stoop 304-metaclasses

S.Ducasse 9

The Meaning of Is-a• Every object is an instance of a class.• When anObject 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).

Page 10: Stoop 304-metaclasses

S.Ducasse 10

A Class is an Object too…So messages sent to a class are looked up into the class of the class

Node withName: #node1Node is an instance of

“Node class” withName: is looked up

in the class “Node class”withName: defined in

“Node class” lookup stops + method executed

Page 11: Stoop 304-metaclasses

S.Ducasse 11

Class Parallel Inheritance

Page 12: Stoop 304-metaclasses

S.Ducasse 12

Lookup and Class Methods

Page 13: Stoop 304-metaclasses

S.Ducasse 13

Class Parallel inheritance•Workstation withName: #mac

• Workstation is an instance of Workstation class

• => withName: is looked up in the class Workstation class

• withName: is not defined in Workstation class • => lookup continues in the superclass of

Workstation class = Node class• withName: is defined in Node class • => lookup stops + method executed

Page 14: Stoop 304-metaclasses

S.Ducasse 14

Objectrepresents 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

Classrepresents 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 instancesSo all the classes should inherit ultimately from Class

Responsibilities of Object & Class

Page 15: Stoop 304-metaclasses

S.Ducasse 15

The kernel of CLOS and ObjVlisp but not the kernel of Smalltalk

A Fragile Reflective Kernel

Page 16: Stoop 304-metaclasses

S.Ducasse 16

Singleton with explicit metaclasses

Page 17: Stoop 304-metaclasses

S.Ducasse 17

Deeper into It

Page 18: Stoop 304-metaclasses

S.Ducasse 18

No explicit metaclasses, only implicit non-sharable metaclasses.

(1) Every class is ultimately a subclass of Object (except Object itself)Object Behavior

ClassDescription Class Metaclass

(II) Every object is an instance of a class = every class is an instance of a class which is its metaclass.

Smalltalk Metaclasses in 7 points

Page 19: Stoop 304-metaclasses

S.Ducasse 19

(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’

Smalltalk Metaclasses in 7 points

Page 20: Stoop 304-metaclasses

S.Ducasse 20

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

Smalltalk Metaclasses in 7 points

Page 21: Stoop 304-metaclasses

S.Ducasse 21

(5) Every metaclass is an instance of Metaclass. So Metaclass is an instance of itself

Object : common object behaviorClass: common class behavior (name, multiple instances)Metaclass: common metaclass behavior (no name, unique instance)

Smalltalk Metaclasses in 7 points

Page 22: Stoop 304-metaclasses

S.Ducasse 22

(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 withName:An instance method defined in Behavior or ClassDescription, is available as a class method. Example: new, new:

Smalltalk Metaclasses in 7 points

Page 23: Stoop 304-metaclasses

S.Ducasse 23

Complete Picture

Page 24: Stoop 304-metaclasses

S.Ducasse 24

Final ThoughtsFinally it is not sure that the Smalltalk model is more complex than the one of ObjVlisp.

If we consider the programmer view of a class, Smalltalk is simpler

If we consider the meta-programmer, ObjVlisp is simpler

Page 25: Stoop 304-metaclasses

S.Ducasse 25

Responsibilities

Page 26: Stoop 304-metaclasses

S.Ducasse 26

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 methodinstance creation (new, basicNew, new:, basicNew:)class into hierarchy ( superclass:, addSubclass:accessing (selectors, allSelectors, compiledMethodAt: )accessing instances and variables (allInstances, instVarNames)accessing class hierarchy (superclass, subclasses)testing (hasMethods, includesSelector, canUnderstand:, inheritsFrom:, isVariable)

Behavior Responsibilities

Page 27: Stoop 304-metaclasses

S.Ducasse 27

ClassDescription adds a number of facilities to basic Behavior:

named instance variablescategory organization for methodsthe notion of a name (abstract)the maintenance of the Changes set, and logging changesmost of the mechanisms needed for fileOutClassDescription is an abstract class: its facilities are intended for inheritance by the two subclasses, Class and Metaclass.

ClassDescription Responsibilities

Page 28: Stoop 304-metaclasses

S.Ducasse 28

Metaclass initialization of class variablescreating initialized instances of the metaclass’s sole instanceinstance creation (subclassOf:)metaclass instance protocol (name:inEnvironment:subclassOf:....)

ClassClass adds naming for classClass adds the representation for classVariable names and shared pool variables (addClassVaraNames, addSharedPool:, initialize)

Metaclass and Class Responsibilities

Page 29: Stoop 304-metaclasses

S.Ducasse 29

SummaryClasses are objects tooA class is the unique instance of another class, its metaclass