04 Object
04 Object
1 Simula
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
TYLA Objects March 20, 2020 3 / 150
Simula
Oct 12, 1931, Aug 27, 1926, Oslo, May 11, 1930,
Mandal, NO NO Rotterdam, NL
June 29, 2002, Asker, Aug 10, 2002, Oslo, Aug 06, 2002,
NO NO Nuenen, NL
“...are there too many “To program is to “Do only what only
basic mechanisms understand!” you can”
floating around doing
nearly the same
Table of Contents
1 Simula
People Behind SIMULA
SIMULA I
Simula 67
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
TYLA Objects March 20, 2020 10 / 150
Simula
An ALGOL 60 preprocessor
A subprogram library
An original per “process” stack allocation scheme
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
TYLA Objects March 20, 2020 15 / 150
Simula 67
Introduces:
Introduces:
I the concept of object,
Introduces:
I the concept of object,
I the concept of class,
Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),
Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),
I the concept of inheritance
(introduced by C. A. R. Hoare for records),
Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),
I the concept of inheritance
(introduced by C. A. R. Hoare for records),
I the concept of virtual method,
Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),
I the concept of inheritance
(introduced by C. A. R. Hoare for records),
I the concept of virtual method,
I attribute hiding!
Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),
I the concept of inheritance
(introduced by C. A. R. Hoare for records),
I the concept of virtual method,
I attribute hiding!
Immense funding problems
steady support from C. A. R. Hoare, N. Wirth and D. Knuth.
Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),
I the concept of inheritance
(introduced by C. A. R. Hoare for records),
I the concept of virtual method,
I attribute hiding!
Immense funding problems
steady support from C. A. R. Hoare, N. Wirth and D. Knuth.
Standardized ISO 1987.
1 Simula
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
Principles:
Everything is object;
Every object is described by its class (structure, behavior);
Message passing is the only interface to objects.
Origin:
A programming language that children can understand;
To create “tomorrow’s computer”: Dynabook.
2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80
4 C++
5 CLOS
6 Prototype-based
TYLA programmingObjects March 20, 2020 25 / 150
Alan Kay
I invented the term Object-Oriented and I can tell you I did not have
C++ in mind.
– A. Kay
2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80
4 C++
5 CLOS
6 Prototype-based
TYLA programmingObjects March 20, 2020 35 / 150
Smalltalk 72
Written in BASIC.
Reuses the classes and instances from Simula 67.
Adds the concept of “message”.
Dynamic method lookup.
Point to ColorPoint
x, y print x, y
print print
x, x <− x, x <−
y, y <− y, y <−
color, color <−
instance instance
instance instance
2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80
4 C++
5 CLOS
6 Prototype-based
TYLA programmingObjects March 20, 2020 40 / 150
Smalltalk 76
Introduction of the Class class.
The class of classes. Instance of itself. Metaclass. How to print
a class, add method, instantiate etc.
Class
Object
Number String
Integer
Superclass
Instance de
Significant improvement:
I Byte-code and a virtual machine provide a 4-100 speedup.
I ThingLab, constraint system experimentation.
I PIE, Personal Information Environment.
Significant improvement:
I Byte-code and a virtual machine provide a 4-100 speedup.
I ThingLab, constraint system experimentation.
I PIE, Personal Information Environment.
But:
I A single metaclass
hence a single behavior for classes
(no specific constructors, etc.).
2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80
4 C++
5 CLOS
6 Prototype-based
TYLA programmingObjects March 20, 2020 44 / 150
Smalltalk 80
Float Object
Magnitude Behavior
Number ClassDescription
Object class
System
Class, Object, Number, Boolean, BlockContext etc.
Programming Environment
Model, View, Controler, etc.
Standard Library
Collection, Stream, etc.
Notable inventions
Bitmap, Mouse, Semaphore, Process, ProcessScheduler
In Magnitude
>= aMagnitude
↑ ( self < aMagnitude ) not
In Date
< aDate
year < aDate year
ifTrue : [ ↑ day < aDate day ]
ifFalse : [ ↑ year < aDate year ]
Collection
Bag Set
SequenceableCollection
Dictionary
LinkedList Interval
ArrayedCollection OrderedCollection
Symbol
In LinkedList:
do : aBlock
| aLink |
aLink <- firstLink .
[ aLink = nil ] whileFalse :
[ aBlock value : aLink .
aLink <- aLink nextLink ]
sum <- 0.
#(2 3 5 7 11) do :
[ : prime |
sum <- sum + ( prime * prime ) ]
or:
sum <- 0.
#(2 3 5 7 11)
collect : [ : prime | prime * prime ];
do : [ : number | sum <- sum + number ]
Validate.
Go into the Complex class, class methods, and create:
re : realPart im : imPart
↑ ( self new ) setRe : realPart setIm : imPart
im
" Return the imaginary part of the receiver .
↑ im
+ aComplex
↑ Complex re : ( re + aComplex re )
im : ( im + aComplex im )
But then:
( Complex re : 42 im : 51) + 666
yields message not understood: re.
TYLA Objects March 20, 2020 62 / 150
Sub-classing in Smalltalk 80: Complexes
https://squeak.org
TYLA Objects March 20, 2020 65 / 150
Table of Contents
1 Simula
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
2 Smalltalk
4 C++
5 CLOS
TYLA Objects March 20, 2020 69 / 150
Bertrand Meyer (1950), MIT
2 Smalltalk
4 C++
5 CLOS
TYLA Objects March 20, 2020 71 / 150
Introducing Eiffel
EiffelCOM (COM,OLE,ActiveX),
EiffelCORBA,
EiffelMath,
EiffelNet (client-serveur),
EiffelLex & EiffelParse,
EiffelStore (BD),
EiffelWEB,
Eiffel DLE (dynamic link),
EiffelVision (GUI),
Graphical Eiffel for Windows, Eiffel WEL (Windows),
EiffelThreads,
etc.
Classes :
I One per file (.e)
I Groupped in clusters
I One one them is the main class
Eiffel Librairies (only one in practice)
External Librairies
A file describing the application
I LACE file, Langage pour l’assemblage des classes en Eiffel
LOGICAL point-of-view
Set of classes building an antonomous part of the application
PHYSICAL point-of-view
All these classes lay in the same repository
LACE point-of-view
A cluster is a name associated to a repository
system
geo
root
TEST ( TEST ): " main "
default
precompiled ( " $EIFFEL3 / precomp / spec / $PLATFORM / base " )
cluster
TEST : " $EIFFELDIR / TEST " ;
FIGS : " $EIFFELDIR / FIGURES " ;
external
object : " $ ( EIFFEL3 )/ library / lex / spec / $ ( PLATFORM )/ lib / lex . a "
end
2 Smalltalk
4 C++
5 CLOS
TYLA Objects March 20, 2020 82 / 150
Example of a Class
class POINT
-- un point dans un dessin g é om é trique
feature
-- deux attributs : les coordonn é es
xc , yc : INTEGER ;
variable
receiving
the eventual
result method method The object execute the
name parameters
method m which is executed
in its own context.
x := o.m(a,b,c,...)
For distributed objects, a
message is sent, otherwise it
object message is a simple procedure call.
receiving the
message
do created object
...
!! p ; −− instruction
...
end ;
class POINT
create Attributes are initialized with
make -- init method a default value (e.g., 0 for an
feature Integer, Void for a variable
-- init method with type reference).
make (x , y : INTEGER ) is
do
set_x_y (x , y ) ;
end ; If we want to initialize an
object during creation, we
-- same as previously must build a initialization
end -- class POINT method
Performances issues . . .
2 Smalltalk
4 C++
5 CLOS
TYLA Objects March 20, 2020 92 / 150
Problem Statement
Simple Inheritance: a
class may herit at most from
only one class
Multiple Inheritance:
more powerful than the
simple inheritance but
introduces problems.
Eiffel proposes the
adaptation clauses to solve
these problems.
Class A Class B
Simple Inheritance: a primitive primitive
class may herit at most from p p
SHAPE
POLYGON
SQUARE COLORED_SHAPE
COLORED_SQUARE
A B C A B C
deferred p deferred p
class D class D
Features:
Rename inherited primitives
Features:
Rename inherited primitives
Modify Visibility of inherited primitives
Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Redefine inherited primitives
Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Redefine inherited primitives
Selection clauses
Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Redefine inherited primitives
Selection clauses
inherit
SHAPE
rename
make as make_shape The renamed primitive is
end ; still accessible but with a
different name.
feature
width : INTEGER ;
The original name can
make (x , y : INTEGER ;
then be used for another
w : INTEGER ) is
primitive even with a
do
different signature.
make_shape (x , y ) ;
width := w ;
end ;
méthode méthode
décrocher() décrocher()
rename
decrocher
as
TELEPHONE_MURAL decrocher_du_mur
inherit
SHAPE
rename make as make_shape
export { NONE } make_shape
end ;
make shape was accessible
without reasons in class
feature
SQUARE
width : INTEGER ;
May help to mask
make (x , y : INTEGER ; inherited primitive
w : INTEGER ) is
do
make_shape (x , y ) ;
width := w ;
end ;
end -- class SQUARE
TYLA Objects March 20, 2020 101 / 150
Access Restrictions
feature ou feature{ANY}
primitives with default access value
(All objects derive from ANY)
feature{A,B,C,...}
primitives with access restricted only to some classes A, B, C
feature{} ou feature{NONE}
unreachable primitives
(NONE : no instance from this classe )
inherit
SHAPE
rename make as make_shape Constraints on redefintions
export { NONE } make_shape
redefine draw Each redefinition must be
end ; declared
feature Redefined methods are
targetted by the dynamic
draw ( g : GRAPHICS ) is
do
lookup
...
end ;
...
inherit
A
rename p as pa end ;
feature
p ... is ...
q () is do p () end ;
class TELEPHONE_MURAL
inherit
TELEPHONE ;
OBJET_MURAL
undefine decrocher
end ;
...
2 Smalltalk
4 C++
5 CLOS
TYLA Objects March 20, 2020 107 / 150
What is that?
class SHAPE
feature
xc , yc : INTEGER ; -- coordinates
set_x_y (x , y : INTEGER ) is
require
x >= 0 and y >= 0
do
xc = x ;
yc = y ;
end ;
...
Pre-conditions in Eiffel
class SHAPE
feature
...
set_x_y (x , y : INTEGER ) is
require
x >= 0 and y >= 0
do
xc := x ;
yc := y ;
ensure
xc = x and yc = y
end ;
Post-conditions in Eiffel
TYLA Objects March 20, 2020 113 / 150
Referencing previous version of an expression
old x reference the value of x before the execution of the method
class RECTANGLE
feature
width , height : INTEGER ;
set_width ( w : INTEGER ) is
require
w > 0
do
width := w
ensure
width = w and height = old height
end ;
...
class RECTANGLE
feature
width , height : INTEGER ;
set_width ( w : INTEGER ) is
-- change the width
require
w > 0
do
width := w
ensure
width = w and strip ( width ) = old strip ( width )
end ;
class A
class B
inherit
The redefined method must A redefine p end ;
satisfy old assertions but
feature
can be more precise: p is
Release some require else
preconditions ... -- other restrictions
do
Add (Restrict) ... -- new defintion
postconditions ensure then
... -- additionnal postco
end ;
end -- class
invariant
( xc < 0 implies width > - xc ) -- visible
and
( yc < 0 implies height > - yy ) -- visible
and
width >= 0
and
height >= 0
-- Code
check
x > 0 ;
y < 0 implies largeur > -y
end ;
1 Simula
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
Bjarne Stroustrup,
BellLabs, 1982.
cfront, a C preprocessor.
G++, the first real C++ compiler.
Standardized in 1998.
introduction of const
introduction of reference
introduction of prototypes
introduction of Booleans
declaring variable anywhere
introduction of void
introduction of inline
introduction of namespace
introduction of overloading etc.
Most features made it into more modern Cs.
1 Simula
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
Overview:
Metaobject Protocol
Meta-class
Multiple Inheritance
Multiple dispatch
Generic Functions
Method Qualifier
Introspection
Economy of execution.
How fast does a program run?
Economy of compilation.
How long does it take to go from sources to executables?
Economy of small-scale development.
How hard must an individual programmer work?
Economy of large-scale development.
How hard must a team of programmers work?
Economy of language features.
How hard is it to learn or use a programming language?
1 Simula
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
Self
Heirs
TYLA Objects March 20, 2020 142 / 150
Problem Statement
Unless one can predict with certainty what qualities a set of objects
and classes will have in the distant future, one cannot design a class
hierarchy properly
Overview:
Neither classes nor meta-classes
Self objects are a collection of slots. Slots are accessor methods
that return values.
Self object is a stand-alone entity
An object can delegate any message it does not understand itself
to the parent object
Inspired from Smalltalks blocks for flow control
Generational garbage collector
Javascript
NewtonScript
Io
Rust
Go
2 Smalltalk
4 C++
5 CLOS
6 Prototype-based programming
Self
Heirs
TYLA Objects March 20, 2020 147 / 150
Rust, Go, ...
Gang of 4 quote
Object-oriented programs are made up of objects. An object
packages both data and the procedures that operate on that data.
The procedures are typically called methods or operations.
–
Elements of Reusable Object-Oriented Software
Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides
Rust’s documentation
Even though structs and enums with methods arent called objects,
they provide the same functionality, according to the Gang of Fours
definition of objects.