7. Understanding Classes and Metaclasses. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed.

Slides:



Advertisements
Similar presentations
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
Advertisements

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
ESE Einführung in Software Engineering 6. Modeling Objects and Classes Prof. O. Nierstrasz.
ESE Einführung in Software Engineering 7. Modeling Behaviour Prof. O. Nierstrasz.
CP — Concurrent Programming 8. Liveness and Asynchrony Prof. O. Nierstrasz Wintersemester 2005 / 2006.
S.Ducasse 1 Metaclasses in 7 Steps Classes are objects too... Classes are instances of other classes... One model applied twice.
OORPT Object-Oriented Reengineering Patterns and Techniques 7. Problem Detection Prof. O. Nierstrasz.
11. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 A bit of Smalltalk Overview  Some history  Smalltalk syntax & object model  The.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Object-Oriented Reengineering Patterns and Techniques Prof. O. Nierstrasz Prof. S. Ducasse T.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
4. Smalltalk Coding Idioms. © Oscar Nierstrasz ST — Smalltalk Coding Idioms 4.2 Roadmap  Snakes and Ladders — Cascade and Yourself  Lots of Little Methods.
Stéphane Ducasse«ChapterNr».1 Arthur Riel Design Heuristics from Object-Oriented Design Heuristics by Arthur Riel Read the Heuristics… –Find reasons why.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Overview  Common errors … —Typical.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
The Software Composition Group Prof. O. Nierstrasz
13. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Summary, Trends, Research...  Summary: functional, logic and object-oriented.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
Stéphane Ducasse 1 The Taste of Smalltalk.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
13. Traits. Selected literature  Cook. Interfaces and Specifications for the Smalltalk-80 Collection Classes. OOPSLA 1992  Taivalsaari. On the Notion.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Some Basic Points on Classes
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
6. Debugging. © Oscar Nierstrasz ST — Debugging 6.2 Roadmap  Common syntactic errors  Common semantic errors  Encapsulation errors  Class/instance.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
10. Reflection. © Oscar Nierstrasz ST — Reflection 10.2 Roadmap  What is reflection?  Object meta-operations  Class meta-operations  Method meta-operations.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap Overview  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
9. Understanding Classes and Metaclasses. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify your metamodel — A fully reflective system models.
6. Debugging. © Oscar Nierstrasz ST — Debugging 6.2 Roadmap  Common syntactic errors  Common semantic errors  Encapsulation errors  Class/instance.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
© Oscar Nierstrasz ST — xxx X.1 Class Methods - Class Instance Variables  Classes (Packet class) represents class (Packet).  Class instance variables.
10. Reflection. © Oscar Nierstrasz ST — Reflection 10.2 Roadmap  What is reflection?  Object meta-operations  Class meta-operations  Method meta-operations.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse«ChapterNr».1 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 MakeMoney Corp. C*O of MakeMoney Corp. Our Vision  We invest in software  We do not know software 
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
12. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 Roadmap  Some history  Smalltalk syntax & object model  The Smalltalk environment.
4. Smalltalk Coding Idioms. © Oscar Nierstrasz ST — Smalltalk Coding Idioms 4.2 Roadmap  Snakes and Ladders — Cascade and Yourself  Lots of Little Methods.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
7. Just In Time Compilation Prof. O. Nierstrasz Jan Kurs.
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Stéphane Ducasse 1 Singleton.
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
Stéphane Ducasse 1 Some Advanced Points on Classes.
1 Advanced Object Oriented Systems (CM0318) Lecture 8 (Last updated 15th February 2002)
3. Standard Classes. © Oscar Nierstrasz ST — Standard Classes 3.2 Roadmap  Object  Numbers, Characters, Strings and Arrays  Variables  Blocks and.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
Smalltalk Meta-Programming Programming Languages HUJI 2010 Yardena Meymann.
Introduction to Smalltalk
Presentation transcript:

7. Understanding Classes and Metaclasses

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed Classes  Class Instance Variables  Class Variables  Pool Dictionaries Selected material courtesy Stéphane Ducasse

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.3 Roadmap  Metaclasses in 7 points  Indexed Classes  Class Instance Variables  Class Variables  Pool Dictionaries

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.4 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass Adapted from Goldberg & Robson, Smalltalk-80 — The Language

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.5 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses Every object is an instance of a class Remember the Snakes and Ladders Board Game …

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.7 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses Every class inherits from Object  Every object is-an Object = —The class of every object ultimately inherits from Object Caveat: in Squeak, Object has a superclass called ProtoObject A snake is-a Snake and is-a board Square and is-an Object

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.9 The Meaning of is-a  When an object receives a message, the method is looked up in the method dictionary of its class, and, if necessary, its superclasses, up to Object

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.10 Responsibilities of Object  Object —represents the common object behavior – error-handling, halting … —all classes should inherit ultimately from Object

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.11 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses Every class is an instance of a metaclass  Classes are objects too! —Every class X is the unique instance of its metaclass, called X class

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.13 Metaclasses are implicit  There are no explicit metaclasses —Metaclasses are created implicitly when classes are created —No sharing of metaclasses (unique metaclass per class)

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.14 Metaclasses by Example Square allSubclasses Snake allSubclasses Snake allInstances Snake instVarNames Snake back: 5 Snake selectors Snake canUnderstand: #new Snake canUnderstand: #setBack: Square allSubclasses Snake allSubclasses Snake allInstances Snake instVarNames Snake back: 5 Snake selectors Snake canUnderstand: #new Snake canUnderstand: #setBack: a Set(Snake FirstSquare Ladder) a Set() an Array(<-6[11] <-2[6] <-4[11]) #('back') <-5[nil] an IdentitySet(#printOn: #destination #setBack:) false true a Set(Snake FirstSquare Ladder) a Set() an Array(<-6[11] <-2[6] <-4[11]) #('back') <-5[nil] an IdentitySet(#printOn: #destination #setBack:) false true

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.15 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses The metaclass hierarchy parallels the class hierarchy

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.17 Uniformity between Classes and Objects  Classes are objects too, so … —Everything that holds for objects holds for classes as well —Same method lookup strategy – Look up in the method dictionary of the metaclass back: is a Snake constructor method

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.18 About the Buttons

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.19 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses Every metaclass inherits from Class and Behavior Every class is-a Class = —The metaclass of every class inherits from Class

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.21 Where is new defined?

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.22 Responsibilities of Behavior  Behavior —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, basicNew, new:, basicNew:) – class hierarchy manipulation (superclass:, addSubclass:) – accessing (selectors, allSelectors, compiledMethodAt: ) – accessing instances and variables (allInstances, instVarNames) – accessing class hierarchy (superclass, subclasses) – testing (hasMethods, includesSelector, canUnderstand:, inheritsFrom:, isVariable)

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.23 Responsibilities of ClassDescription  ClassDescription —adds a number of facilities to basic Behavior: – named instance variables – category organization for methods – the notion of a name (abstract) – maintenance of Change sets and logging changes – most of the mechanisms needed for fileOut —ClassDescription is an abstract class: its facilities are intended for inheritance by the two subclasses, Class and Metaclass.

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.24 Responsibilities of Class  Class —represents the common behavior of all classes – name, compilation, method storing, instance variables … —representation for classVariable names and shared pool variables (addClassVarName:, addSharedPool:, initialize) —Class inherits from Object because Class is an Object – Class knows how to create instances, so all metaclasses should inherit ultimately from Class

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.25 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses Every metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.27 Metaclass Responsibilities  Metaclass —Represents common metaclass Behavior – instance creation (subclassOf:) – creating initialized instances of the metaclass’s sole instance – initialization of class variables – metaclass instance protocol (name:inEnvironment:subclassOf:....) – method compilation (different semantics can be introduced) – class information (inheritance link, instance variable,...)

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.28 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 and Behavior 6. Every metaclass is an instance of Metaclass 7. The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses The metaclass of Metaclass is an instance of Metaclass

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.30 Navigating the metaclass hierarchy MetaclassHierarchyTest>>testHierarchy "The class hierarchy" self assert: Snake superclass = Square. self assert: Square superclass = Object. self assert: Object superclass superclass = nil. "skip ProtoObject" "The parallel metaclass hierarchy" self assert: Snake class name = 'Snake class'. self assert: Snake class superclass = Square class. self assert: Square class superclass = Object class. self assert: Object class superclass superclass = Class. self assert: Class superclass = ClassDescription. self assert: ClassDescription superclass = Behavior. self assert: Behavior superclass = Object. "The Metaclass hierarchy" self assert: Snake class class = Metaclass. self assert: Square class class = Metaclass. self assert: Object class class = Metaclass. self assert: Class class class = Metaclass. self assert: ClassDescription class class = Metaclass. self assert: Behavior class class = Metaclass. self assert: Metaclass superclass = ClassDescription. "The fixpoint" self assert: Metaclass class class = Metaclass. MetaclassHierarchyTest>>testHierarchy "The class hierarchy" self assert: Snake superclass = Square. self assert: Square superclass = Object. self assert: Object superclass superclass = nil. "skip ProtoObject" "The parallel metaclass hierarchy" self assert: Snake class name = 'Snake class'. self assert: Snake class superclass = Square class. self assert: Square class superclass = Object class. self assert: Object class superclass superclass = Class. self assert: Class superclass = ClassDescription. self assert: ClassDescription superclass = Behavior. self assert: Behavior superclass = Object. "The Metaclass hierarchy" self assert: Snake class class = Metaclass. self assert: Square class class = Metaclass. self assert: Object class class = Metaclass. self assert: Class class class = Metaclass. self assert: ClassDescription class class = Metaclass. self assert: Behavior class class = Metaclass. self assert: Metaclass superclass = ClassDescription. "The fixpoint" self assert: Metaclass class class = Metaclass.

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.31 Roadmap  Metaclasses in 7 points  Indexed Classes  Class Instance Variables  Class Variables  Pool Dictionaries

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.32 Two ways to represent objects  Named or indexed instance variables —Named: name of GamePlayer —Indexed: #(Jack Jill) at: 1  Or looking at them in another way: —Objects with pointers to other objects —Objects with arrays of bytes (word, long) —Difference for efficiency reasons: – arrays of bytes (like C strings) are faster than storing an array of pointers, each pointing to a single byte.

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.33 Different methods to create classes Indexed Named Instance Variables Definition Method NoYes#subclass: … Yes #variableSubclass: … YesNo#variableByteSubclass: …  See the subclass creation protocol of Class  Constraints —Pointer classes defined using #subclass: support any kind of subclasses —Byte classes defined using #variableSubclass: can only have: variableSubclass: or variableByteSubclass: subclasses

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.34 Testing methods  See testing protocols of Behavior: — #isPointers, #isBits, #isBytes, #isFixed, #isVariable — #kindOfSubclass Object allSubclasses select: [:class | class isBytes] a Set(ByteArray MwSynchronizationWrapper MwBlockMethodWrapper ExternalAddress MCMockClassH LargeNegativeInteger LargePositiveInteger ByteSymbol MwCountMethodWrapper MwTimeMethodWrapper MwMethodWrapper MwBlockHandlerMethodWrapper ByteString MwCalledMethodWrapper UUID CompiledMethod)

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.35 Defining Indexed Classes  Example — instantiating an Array: ArrayedCollection variableSubclass: #Array instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Collections-Arrayed' ArrayedCollection variableSubclass: #Array instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Collections-Arrayed' Array new: 4 #(nil nil nil nil) #( ) class isVariable true

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.36 Defining an Indexed Class Object variableSubclass: #IndexedObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: '' Object variableSubclass: #IndexedObject instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: '' (IndexedObject new: 2) at: 1 put: 'Jack'; at: 2 put: 'Jill'; at: 1 (IndexedObject new: 2) at: 1 put: 'Jack'; at: 2 put: 'Jill'; at: 1 'Jack'

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.37 Indexed Classes / Instance Variables  An indexed variable is implicitly added to the list of instance variables —Only one indexed instance variable per class —Access with at: and at:put: – NB: answers the value, not the receiver  Subclasses should also be indexed

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.38 Roadmap  Metaclasses in 7 points  Indexed Classes  Class Instance Variables  Class Variables  Pool Dictionaries

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.39 Class Instance Variables  Class are objects too —Instances of their metaclass – Methods looked up in the method dictionary of their metaclass —Can also define instance variables  When a metaclass defines a new instance variable, then its instance (a Class) gets a new variable —I.e., in addition to subclass, superclasses, methodDict…  Use class instance variables to represent the private state of the class —E.g., number of instances, superclass etc. – Not to represent information shared by all instances!

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.40 Example: the Singleton Pattern  A class having only one instance —We keep the unique instance created in an instance variable WebServer class instanceVariableNames: 'uniqueInstance’ WebServer class>>new self error: 'Use uniqueInstance to get the unique instance' WebServer class>>uniqueInstance uniqueInstance isNil ifTrue: [uniqueInstance := self basicNew initialize]. ^ uniqueInstance WebServer class instanceVariableNames: 'uniqueInstance’ WebServer class>>new self error: 'Use uniqueInstance to get the unique instance' WebServer class>>uniqueInstance uniqueInstance isNil ifTrue: [uniqueInstance := self basicNew initialize]. ^ uniqueInstance

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.41 Roadmap  Metaclasses in 7 points  Indexed Classes  Class Instance Variables  Class Variables  Pool Dictionaries

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.42 Class Variable = Shared Variable  To share information amongst all instances of a class, use a “class variable” —Shared and directly accessible by all the instances of the class and subclasses —Accessible to both instance and class methods —Begins with an uppercase letter

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.43 Initializing class variables  Class variables should be initialized by an initialize method on the class side Magnitude subclass: #Date instanceVariableNames: 'julianDayNumber ' classVariableNames: 'DaysInMonth FirstDayOfMonth MonthNames SecondsInDay WeekDayNames ' poolDictionaries: '' category: 'Kernel-Magnitudes' Date class>>initialize... WeekDayNames := #(Sunday Monday...). MonthNames := #(January February... ). DaysInMonth := #( ). Magnitude subclass: #Date instanceVariableNames: 'julianDayNumber ' classVariableNames: 'DaysInMonth FirstDayOfMonth MonthNames SecondsInDay WeekDayNames ' poolDictionaries: '' category: 'Kernel-Magnitudes' Date class>>initialize... WeekDayNames := #(Sunday Monday...). MonthNames := #(January February... ). DaysInMonth := #( ).

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.44 ClassVariables vs. Instance Variables

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.45 Roadmap  Metaclasses in 7 points  Indexed Classes  Class Instance Variables  Class Variables  Pool Dictionaries

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.46 Pool Dictionaries  A Pool Dictionary is a shared variable —Begins with a uppercase letter. —Shared by a group of classes not linked by inheritance.  Each class possesses its own pool dictionary (containing pool variables). —They are not inherited.  Don’t use them!

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.47 Examples of Pool Dictionaries ArrayedCollection subclass: #Text instanceVariableNames: 'string runs' classVariableNames: '' poolDictionaries: 'TextConstants' category: 'Collections-Text' ArrayedCollection subclass: #Text instanceVariableNames: 'string runs' classVariableNames: '' poolDictionaries: 'TextConstants' category: 'Collections-Text'  Elements stored into TextConstants like Ctrl, CR, ESC, Space can be directly accessed from all the classes like ParagraphEditor....  Hint: You can inspect any Pool Dictionary

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.48 Smalltalk System Dictionary  Pool Dictionaries are stored in the Smalltalk system dictionary Smalltalk inspect (Smalltalk at: #TextConstants) at: #ESC $ $

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.49 Accessing globals  Use message-sending instead of directly accessing pool variables stream nextPut: Lf "A pool variable visible to the class" stream nextPut: Character lf vs.

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.50 What you should know!  What does is-a mean?  What is the difference between sending a message to an object and to its class?  What are the responsibilities of a metaclass?  What is the superclass of Object class?  Where is new defined?  What is the difference between class variables and class instance variables?

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.51 Can you answer these questions?  Why are there no explicit metaclasses?  When should you override new?  Why don’t metaclasses inherit from Class?  Are there any classes that don’t inherit from Object?  Is Metaclass a Class? Why or why not?  Where are the methods class and superclass defined?  When should you define an indexed class?  Are Java static variables just like class variables or class instance variables?  Where is the SystemDictionary Smalltalk defined?

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.52 License > Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. Attribution-ShareAlike 2.5 You are free: to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions: Attribution. You must attribute the work in the manner specified by the author or licensor. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above.