SlideShare a Scribd company logo
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Classes and Metaclasses
- an Analysis
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
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
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
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”
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
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
S.Ducasse 8
Node allSubclasses -> OrderedCollection (WorkStation
OutputServer Workstation
FileServer PrintServer)
PrintServer allInstances -> ()
Node instVarNames -> ('name' 'nextNode')
Workstation withName: mac -> aWorkstation
Workstation selectors -> IdentitySet (accept: originate:)
Workstation canUnderstand: nextNode -> true
Metaclass by Example
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).
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: #node1
Node is an instance of
“Node class”
withName: is looked up
in the class “Node class”
withName: defined in
“Node class”
lookup stops +
method executed
S.Ducasse 11
Class Parallel Inheritance
S.Ducasse 12
Lookup and Class Methods
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
S.Ducasse 14
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
Responsibilities of Object & Class
S.Ducasse 15
The kernel of CLOS and ObjVlisp but not the kernel of
Smalltalk
A Fragile Reflective Kernel
S.Ducasse 16
Singleton with explicit metaclasses
S.Ducasse 17
Deeper into It
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
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
S.Ducasse 20
If X is a subclass ofY then X class is a subclass ofY 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
S.Ducasse 21
(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)
Smalltalk Metaclasses in 7 points
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
S.Ducasse 23
Complete Picture
S.Ducasse 24
Final Thoughts
Finally 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
S.Ducasse 25
Responsibilities
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 method
instance 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
S.Ducasse 27
ClassDescription 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 fileOut
ClassDescription is an abstract class: its facilities are
intended for inheritance by the two subclasses, Class and
Metaclass.
ClassDescription Responsibilities
S.Ducasse 28
Metaclass
initialization of class variables
creating initialized instances of the metaclass’s sole instance
instance creation (subclassOf:)
metaclass instance protocol
(name:inEnvironment:subclassOf:....)
Class
Class adds naming for class
Class adds the representation for classVariable names and
shared pool variables (addClassVaraNames, addSharedPool:,
initialize)
Metaclass and Class Responsibilities
S.Ducasse 29
Summary
Classes are objects too
A class is the unique instance of another class, its
metaclass

More Related Content

Similar to Stoop 304-metaclasses

Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
The World of Smalltalk
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
The World of Smalltalk
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
The World of Smalltalk
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
The World of Smalltalk
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
The World of Smalltalk
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Bill Buchan
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
The World of Smalltalk
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
The World of Smalltalk
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
The World of Smalltalk
 
2 - OOP
2 - OOP2 - OOP
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
The World of Smalltalk
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
The World of Smalltalk
 
Ruby's metaclass
Ruby's metaclassRuby's metaclass
Ruby's metaclass
xds2000
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
The World of Smalltalk
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
Chamnap Chhorn
 
Proyecto
ProyectoProyecto
1 - OOP
1 - OOP1 - OOP
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
SeethaDinesh
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
DevaKumari Vijay
 

Similar to Stoop 304-metaclasses (20)

Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)4 - OOP - Taste of Smalltalk (Squeak)
4 - OOP - Taste of Smalltalk (Squeak)
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
2 - OOP
2 - OOP2 - OOP
2 - OOP
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
Ruby's metaclass
Ruby's metaclassRuby's metaclass
Ruby's metaclass
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Proyecto
ProyectoProyecto
Proyecto
 
1 - OOP
1 - OOP1 - OOP
1 - OOP
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 

More from The World of Smalltalk

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
The World of Smalltalk
 
99 questions
99 questions99 questions
13 traits
13 traits13 traits
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
The World of Smalltalk
 
11 bytecode
11 bytecode11 bytecode
10 reflection
10 reflection10 reflection
10 reflection
The World of Smalltalk
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
The World of Smalltalk
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
The World of Smalltalk
 
06 debugging
06 debugging06 debugging
05 seaside
05 seaside05 seaside
04 idioms
04 idioms04 idioms
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
The World of Smalltalk
 
02 basics
02 basics02 basics
01 intro
01 intro01 intro
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
The World of Smalltalk
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
The World of Smalltalk
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
The World of Smalltalk
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
The World of Smalltalk
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
The World of Smalltalk
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
The World of Smalltalk
 

More from The World of Smalltalk (20)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 

Recently uploaded

Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
Fwdays
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
Vadym Kazulkin
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
ScyllaDB
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
Edge AI and Vision Alliance
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
operationspcvita
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
DanBrown980551
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
Pablo Gómez Abajo
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
AstuteBusiness
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
christinelarrosa
 

Recently uploaded (20)

Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
"Scaling RAG Applications to serve millions of users",  Kevin Goedecke"Scaling RAG Applications to serve millions of users",  Kevin Goedecke
"Scaling RAG Applications to serve millions of users", Kevin Goedecke
 
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024High performance Serverless Java on AWS- GoTo Amsterdam 2024
High performance Serverless Java on AWS- GoTo Amsterdam 2024
 
A Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's ArchitectureA Deep Dive into ScyllaDB's Architecture
A Deep Dive into ScyllaDB's Architecture
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
“Temporal Event Neural Networks: A More Efficient Alternative to the Transfor...
 
The Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptxThe Microsoft 365 Migration Tutorial For Beginner.pptx
The Microsoft 365 Migration Tutorial For Beginner.pptx
 
Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024Northern Engraving | Nameplate Manufacturing Process - 2024
Northern Engraving | Nameplate Manufacturing Process - 2024
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Mutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented ChatbotsMutation Testing for Task-Oriented Chatbots
Mutation Testing for Task-Oriented Chatbots
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |Astute Business Solutions | Oracle Cloud Partner |
Astute Business Solutions | Oracle Cloud Partner |
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
Christine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptxChristine's Product Research Presentation.pptx
Christine's Product Research Presentation.pptx
 

Stoop 304-metaclasses

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Classes and Metaclasses - an Analysis
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. 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
  • 4. 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
  • 5. 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”
  • 6. 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
  • 7. 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
  • 8. S.Ducasse 8 Node allSubclasses -> OrderedCollection (WorkStation OutputServer Workstation FileServer PrintServer) PrintServer allInstances -> () Node instVarNames -> ('name' 'nextNode') Workstation withName: mac -> aWorkstation Workstation selectors -> IdentitySet (accept: originate:) Workstation canUnderstand: nextNode -> true Metaclass by Example
  • 9. 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).
  • 10. 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: #node1 Node is an instance of “Node class” withName: is looked up in the class “Node class” withName: defined in “Node class” lookup stops + method executed
  • 12. S.Ducasse 12 Lookup and Class Methods
  • 13. 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
  • 14. S.Ducasse 14 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 Responsibilities of Object & Class
  • 15. S.Ducasse 15 The kernel of CLOS and ObjVlisp but not the kernel of Smalltalk A Fragile Reflective Kernel
  • 16. S.Ducasse 16 Singleton with explicit metaclasses
  • 18. 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
  • 19. 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
  • 20. S.Ducasse 20 If X is a subclass ofY then X class is a subclass ofY 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
  • 21. S.Ducasse 21 (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) Smalltalk Metaclasses in 7 points
  • 22. 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
  • 24. S.Ducasse 24 Final Thoughts Finally 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
  • 26. 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 method instance 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
  • 27. S.Ducasse 27 ClassDescription 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 fileOut ClassDescription is an abstract class: its facilities are intended for inheritance by the two subclasses, Class and Metaclass. ClassDescription Responsibilities
  • 28. S.Ducasse 28 Metaclass initialization of class variables creating initialized instances of the metaclass’s sole instance instance creation (subclassOf:) metaclass instance protocol (name:inEnvironment:subclassOf:....) Class Class adds naming for class Class adds the representation for classVariable names and shared pool variables (addClassVaraNames, addSharedPool:, initialize) Metaclass and Class Responsibilities
  • 29. S.Ducasse 29 Summary Classes are objects too A class is the unique instance of another class, its metaclass