0% found this document useful (0 votes)
8 views

04 Object

Uploaded by

Gabriel Grandet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

04 Object

Uploaded by

Gabriel Grandet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 193

Objects

Akim Demaille, Etienne Renault, Roland Levillain

March 20, 2020

TYLA Objects March 20, 2020 1 / 150


Table of Contents

1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming

TYLA Objects March 20, 2020 2 / 150


Table of Contents
1 Simula
People Behind SIMULA
SIMULA I
Simula 67

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming
TYLA Objects March 20, 2020 3 / 150
Simula

Figure: Ole-Johan Dahl


TYLA Objects March 20, 2020 4 / 150
Simula

Figure: Ole-Johan Dahl


TYLA Objects March 20, 2020 5 / 150
Simula

Figure: Dahl & Nygaard


TYLA Objects March 20, 2020 6 / 150
Simula

Figure: Ole-Johan Dahl & Kristen Nygaard (ca. 1963)


TYLA Objects March 20, 2020 7 / 150
Simula

Figure: Nygaard & Dahl: Turing Award 2001


TYLA Objects March 20, 2020 8 / 150
2002... Sad Year
Ole-Johan Dahl Kristen Nygaard Edsger Wybe
Dijkstra

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

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming
TYLA Objects March 20, 2020 10 / 150
Simula

In the spring of 1967 a new employee at the NCC in a very


shocked voice told the switchboard operator: “two men are
fighting violently in front of the blackboard in the upstairs
corridor. What shall we do?” The operator came out of her
office, listened for a few seconds and then said: “Relax, it’s
only Dahl and Nygaard discussing SIMULA.” — Kristen
Nygaard, Ole-Johan Dahl.
Physical system models. Norwegian nuclear power plant program.

TYLA Objects March 20, 2020 11 / 150


Simula

In the spring of 1967 a new employee at the NCC in a very


shocked voice told the switchboard operator: “two men are
fighting violently in front of the blackboard in the upstairs
corridor. What shall we do?” The operator came out of her
office, listened for a few seconds and then said: “Relax, it’s
only Dahl and Nygaard discussing SIMULA.” — Kristen
Nygaard, Ole-Johan Dahl.
Physical system models. Norwegian nuclear power plant program.

Process oriented discrete simulation language based on Algol 60.


(1964 - 1965) Simulation language.

TYLA Objects March 20, 2020 11 / 150


Basic concepts (1/2)

A system, consisting of a finite and fixed number of


active components named stations, and a finite, but possibly
variable number of passive components named customers.

TYLA Objects March 20, 2020 12 / 150


Basic concepts (1/2)

A system, consisting of a finite and fixed number of


active components named stations, and a finite, but possibly
variable number of passive components named customers.
A station consisting of two parts: a queue part and a
service part. Actions associated with the service part, named the
station’s operating rule, were described by a sequence of
ALGOL (or ALGOL-like) statements.

TYLA Objects March 20, 2020 12 / 150


Basic concepts (1/2)

A system, consisting of a finite and fixed number of


active components named stations, and a finite, but possibly
variable number of passive components named customers.
A station consisting of two parts: a queue part and a
service part. Actions associated with the service part, named the
station’s operating rule, were described by a sequence of
ALGOL (or ALGOL-like) statements.
A customer with no operating rule, but possibly a finite number
of variables, named characteristics .

TYLA Objects March 20, 2020 12 / 150


Basic concepts (1/2)

A system, consisting of a finite and fixed number of


active components named stations, and a finite, but possibly
variable number of passive components named customers.
A station consisting of two parts: a queue part and a
service part. Actions associated with the service part, named the
station’s operating rule, were described by a sequence of
ALGOL (or ALGOL-like) statements.
A customer with no operating rule, but possibly a finite number
of variables, named characteristics .
A real, continuous variable called time and a function
position, defined for all customers and all values of time.

TYLA Objects March 20, 2020 12 / 150


Basic concepts (2/2)
This structure may be regarded as a network, and the events
(actions) of the stations’ service parts are regarded as instantaneous
and occurring at discrete points of time, this class of systems
was named discrete event networks.

TYLA Objects March 20, 2020 13 / 150


Simula I

An ALGOL 60 preprocessor
A subprogram library
An original per “process” stack allocation scheme

Not yet the concept of objects.

Quasi-parallel processing is analogous to the notion of coroutines


described by Conway in 1963.

TYLA Objects March 20, 2020 14 / 150


Table of Contents
1 Simula
People Behind SIMULA
SIMULA I
Simula 67

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming
TYLA Objects March 20, 2020 15 / 150
Simula 67

Introduces:

TYLA Objects March 20, 2020 16 / 150


Simula 67

Introduces:
I the concept of object,

TYLA Objects March 20, 2020 16 / 150


Simula 67

Introduces:
I the concept of object,
I the concept of class,

TYLA Objects March 20, 2020 16 / 150


Simula 67

Introduces:
I the concept of object,
I the concept of class,
I literal objects (constructors),

TYLA Objects March 20, 2020 16 / 150


Simula 67

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),

TYLA Objects March 20, 2020 16 / 150


Simula 67

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,

TYLA Objects March 20, 2020 16 / 150


Simula 67

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!

TYLA Objects March 20, 2020 16 / 150


Simula 67

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.

TYLA Objects March 20, 2020 16 / 150


Simula 67

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.

TYLA Objects March 20, 2020 16 / 150


Shape in Simula (1/5)
class Shape (x , y ) ; integer x ; integer y ;
virtual : procedure draw is procedure draw ; ;
begin
comment -- get the x & y components for the object --;
integer procedure getX ;
getX := x ;
integer procedure getY ;
getY := y ;
comment -- set the x & y coordinates for the object --;
integer procedure setX ( newx ) ; integer newx ;
x := newx ;
integer procedure setY ( newy ) ; integer newy ;
y := newy ;
comment -- move the x & y position of the object --;
procedure moveTo ( newx , newy ) ; integer newx ; integer newy ;
begin
setX ( newx ) ;
setY ( newy ) ;
end moveTo ;
procedure rMoveTo ( deltax , deltay ) ; integer deltax ; integer deltay ;
begin
setX ( deltax + getX ) ;
setY ( deltay + getY ) ;
end moveTo ;
end Shape ;
Shape in Simula (2/5)
Shape class Rectangle ( width , height ) ;
integer width ; integer height ;
begin
comment -- get the width & height of the object --;
integer procedure getWidth ;
getWidth := width ;
integer procedure getHeight ;
getHeight := height ;
comment -- set the width & height of the object --;
integer procedure setWidth ( newwidth ) ; integer newwidth ;
width := newwidth ;
integer procedure setHeight ( newheight ) ; integer newheight ;
height := newheight ;
comment -- draw the rectangle --;
procedure draw ;
begin
Outtext ( " Drawing a Rectangle at :( " ) ;
Outint ( getX , 0) ; Outtext ( " ," ) ; Outint ( getY , 0) ;
Outtext ( " ) , width " ) ; Outint ( getWidth , 0) ;
Outtext ( " , height " ) ; Outint ( getHeight , 0) ;
Outimage ;
end draw ;
end Rectangle ;
Shape in Simula (3/5)
Shape class Circle ( radius ) ; integer radius ;
begin
comment -- get the radius of the object --;
integer procedure getRadius ;
getRadius := radius ;

comment -- set the radius of the object --;


integer procedure setRadius ( newradius ) ; integer newradius ;
radius := newradius ;

comment -- draw the circle --;


procedure draw ;
begin
Outtext ( " Drawing a Circle at :( " ) ;
Outint ( getX , 0) ;
Outtext ( " ," ) ;
Outint ( getY , 0) ;
Outtext ( " ) , radius " ) ;
Outint ( getRadius , 0) ;
Outimage ;
end draw ;
end Circle ;
Shape in Simula (4/5)
comment -- declare the variables used --;
ref ( Shape ) array scribble (1:2) ;
ref ( Rectangle ) arectangle ;
integer i ;

comment -- populate the array with various shape instances --;


scribble (1) : - new Rectangle (10 , 20 , 5 , 6) ;
scribble (2) : - new Circle (15 , 25 , 8) ;

comment -- iterate on the list , handle shapes p ol ym or p hi ca ll y --;


for i := 1 step 1 until 2 do
begin
scribble ( i ). draw ;
scribble ( i ). rMoveTo (100 , 100) ;
scribble ( i ). draw ;
end ;

comment -- call a rectangle specific instance --;


arectangle : - new Rectangle (0 , 0 , 15 , 15) ;
arectangle . draw ;
arectangle . setWidth (30) ;
arectangle . draw ;
Shape in Simula – Execution (5/5)

> cim shape . sim


Compiling shape . sim :
gcc -g - O2 -c shape . c
gcc -g - O2 -o shape shape . o -L / usr / local / lib - lcim
> ./ shape
Drawing a Rectangle at :(10 ,20) , width 5 , height 6
Drawing a Rectangle at :(110 ,120) , width 5 , height 6
Drawing a Circle at :(15 ,25) , radius 8
Drawing a Circle at :(115 ,125) , radius 8
Drawing a Rectangle at :(0 ,0) , width 15 , height 15
Drawing a Rectangle at :(0 ,0) , width 30 , height 15

TYLA Objects March 20, 2020 21 / 150


Impact of Simula 67

All the object-oriented languages inherit from Simula.


Smalltalk further with object orientation,
further with dynamic binding.

TYLA Objects March 20, 2020 22 / 150


Impact of Simula 67

All the object-oriented languages inherit from Simula.


Smalltalk further with object orientation,
further with dynamic binding.
Objective-C, Pascal, C++, etc.
further with messages.

TYLA Objects March 20, 2020 22 / 150


Impact of Simula 67

All the object-oriented languages inherit from Simula.


Smalltalk further with object orientation,
further with dynamic binding.
Objective-C, Pascal, C++, etc.
further with messages.
CLOS further with method selections.

TYLA Objects March 20, 2020 22 / 150


Impact of Simula 67

All the object-oriented languages inherit from Simula.


Smalltalk further with object orientation,
further with dynamic binding.
Objective-C, Pascal, C++, etc.
further with messages.
CLOS further with method selections.
Eiffel further with software engineering,
further with inheritance.

TYLA Objects March 20, 2020 22 / 150


Impact of Simula 67

All the object-oriented languages inherit from Simula.


Smalltalk further with object orientation,
further with dynamic binding.
Objective-C, Pascal, C++, etc.
further with messages.
CLOS further with method selections.
Eiffel further with software engineering,
further with inheritance.
C++ further with static typing and static binding,
deeper in the *.

TYLA Objects March 20, 2020 22 / 150


Impact of Simula 67

All the object-oriented languages inherit from Simula.


Smalltalk further with object orientation,
further with dynamic binding.
Objective-C, Pascal, C++, etc.
further with messages.
CLOS further with method selections.
Eiffel further with software engineering,
further with inheritance.
C++ further with static typing and static binding,
deeper in the *.
Hybrid languages logic, functional, assembly, stack based etc.

TYLA Objects March 20, 2020 22 / 150


Table of Contents

1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming

TYLA Objects March 20, 2020 23 / 150


Smalltalk

We called Smalltalk Smalltalk so that nobody would expect anything


from it.
– Alan Kay

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.

TYLA Objects March 20, 2020 24 / 150


Table of Contents
1 Simula

2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based
TYLA programmingObjects March 20, 2020 25 / 150
Alan Kay

TYLA Objects March 20, 2020 26 / 150


Quote

I invented the term Object-Oriented and I can tell you I did not have
C++ in mind.
– A. Kay

TYLA Objects March 20, 2020 27 / 150


Alan Kay, 1984

TYLA Objects March 20, 2020 28 / 150


Alan Kay

TYLA Objects March 20, 2020 29 / 150


Ivan Sutherland’s Sketchpad 1967

TYLA Objects March 20, 2020 30 / 150


Douglas Engelbart’s NLS 1974

TYLA Objects March 20, 2020 31 / 150


Flex Machine 1967

TYLA Objects March 20, 2020 32 / 150


DynaBook
It would have, ”enough power
to outrace your senses of sight
and hearing, enough capacity to
store for later retrieval
thousands of page-equivalents
of reference material, poems,
letter, recipes, records,
drawings, animations, musical
scores, waveforms, dynamic
simulations, and anything else
you would like to remember and
change...”.

TYLA Objects March 20, 2020 33 / 150


DynaBook
It would have, ”enough power
to outrace your senses of sight
and hearing, enough capacity to
store for later retrieval
thousands of page-equivalents
of reference material, poems,
letter, recipes, records,
drawings, animations, musical
scores, waveforms, dynamic
simulations, and anything else
you would like to remember and
change...”.
To put this project in context, the smallest general purpose computer
in the early 1970s was about the size of a desk and the word
“multimedia” meant a slide-tape presentation.
TYLA Objects March 20, 2020 33 / 150
DynaBook

TYLA Objects March 20, 2020 34 / 150


Table of Contents
1 Simula

2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80

3 The mysterious language

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.

TYLA Objects March 20, 2020 36 / 150


Smalltalk 72 Sample
to Point
| x y
(
isnew = > (" x <- :.
" y <- :.)
<) x
= > ( <) <- = > (" x <- : )
^ x)
<) y
= > ( <) <- = > (" y <- : )
^ y)
<) print = > ("( print .
x print . center <- Point 0
" , print . = > (0 ,0)
y print . center x <- 3
TYLA
") Objects
print .) = > (3 ,0)
March 20, 2020 37 / 150
Classes and Instances in Smalltalk 72

Point to ColorPoint
x, y print x, y
print print
x, x <− x, x <−
y, y <− y, y <−
color, color <−

instance instance
instance instance

TYLA Objects March 20, 2020 38 / 150


Smalltalk 72 Criticisms

to is a primitive, not a method.


A class is not an object.
The programmer implements the method lookup.
Method lookup is too slow.
No inheritance.
⇒ Programmers were using global procedures.
But some successes:
Pygmalion
“Programming by examples”
inspired Star.

TYLA Objects March 20, 2020 39 / 150


Table of Contents
1 Simula

2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80

3 The mysterious language

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.

TYLA Objects March 20, 2020 41 / 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.
Introduction of the Object class.
Default behavior, shared between all the objects.

TYLA Objects March 20, 2020 41 / 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.
Introduction of the Object class.
Default behavior, shared between all the objects.
Introduction of dictionaries.
Message handling is no longer handled by the programmers.

TYLA Objects March 20, 2020 41 / 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.
Introduction of the Object class.
Default behavior, shared between all the objects.
Introduction of dictionaries.
Message handling is no longer handled by the programmers.
Introduction of inheritance.

TYLA Objects March 20, 2020 41 / 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.
Introduction of the Object class.
Default behavior, shared between all the objects.
Introduction of dictionaries.
Message handling is no longer handled by the programmers.
Introduction of inheritance.
Removal of the to primitive.
Replaced by the new message sent to Class:
Class new title : ’ Rectangle ’;
fields : ’ origin corner ’.

TYLA Objects March 20, 2020 41 / 150


Instantiation, inheritance in Smalltalk 76

Class

Object

Number String

Integer
Superclass
Instance de

Objects keep a link with their generator: is-instance-of


TYLA Objects March 20, 2020 42 / 150
Smalltalk 76 Criticism

Significant improvement:
I Byte-code and a virtual machine provide a 4-100 speedup.
I ThingLab, constraint system experimentation.
I PIE, Personal Information Environment.

TYLA Objects March 20, 2020 43 / 150


Smalltalk 76 Criticism

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

TYLA Objects March 20, 2020 43 / 150


Table of Contents
1 Simula

2 Smalltalk
The People Behind Smalltalk
Smalltalk 72
Smalltalk 76
Smalltalk 80

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based
TYLA programmingObjects March 20, 2020 44 / 150
Smalltalk 80

Deep impact over computer science of the 80’s.


Most constructors take part
(Apple, Apollo, DEC, HP, Tektronix...).
Generalization of the metaclass concept.

TYLA Objects March 20, 2020 45 / 150


Is-instance-of in Smalltalk 80
Metaclass class

Metaclass Class class

Float class Object Class Class

Float Object

Three layer model:


Metaclass. Class behavior (instantiation, initialization, etc.).
Class. Type and behavior of objects.
Instances. The objects.
TYLA Objects March 20, 2020 46 / 150
Inheritance in Smalltalk 80
Object

Magnitude Behavior

Number ClassDescription

Float Metaclass Class

Object class

Magnitude class Behavior class

Number class ClassDescription class

Float class Metaclass class Class class

TYLA Objects March 20, 2020 47 / 150


The Smalltalk 80 System

More than a language, a system where everything is an object, and


the only control structure is message passing.
a virtual image;

TYLA Objects March 20, 2020 48 / 150


The Smalltalk 80 System

More than a language, a system where everything is an object, and


the only control structure is message passing.
a virtual image;
a byte-code compiler;

TYLA Objects March 20, 2020 48 / 150


The Smalltalk 80 System

More than a language, a system where everything is an object, and


the only control structure is message passing.
a virtual image;
a byte-code compiler;
a virtual machine;

TYLA Objects March 20, 2020 48 / 150


The Smalltalk 80 System

More than a language, a system where everything is an object, and


the only control structure is message passing.
a virtual image;
a byte-code compiler;
a virtual machine;
more than 500 classes, 4000 methods, 15000 objects.

TYLA Objects March 20, 2020 48 / 150


Smalltalk 80 Standard Library

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

TYLA Objects March 20, 2020 49 / 150


Smalltalk 80

TYLA Objects March 20, 2020 50 / 150


Smalltalk 80

TYLA Objects March 20, 2020 51 / 150


Booleans: Logical Operators

Boolean methods: and:, or:, not:.


In the True class
and : aBlock
" Evaluate aBlock "
↑ aBlock value
In the False class
and : aBlock
" Return receiver "
↑ self

TYLA Objects March 20, 2020 52 / 150


Booleans: Control Structures
More Boolean methods:
ifTrue:
ifFalse:
ifTrue:ifFalse:
... ifFalse:ifTrue:
For instance, compute a minimum:
| a b x |
...
a <= b ifTrue : [ x <- a ]
ifFalse : [ x <- b ].
...

TYLA Objects March 20, 2020 53 / 150


Integers in Smalltalk 80
Object

Magnitude (>, >=, <=)

Number Date (=, <) Character (=, <)

Float (=, <) Integer (=, <) Fraction (=, <)

SmallInteger (=, <) LargeNegativeInteger (=, <)

LargePositiveInteger (=, <)

TYLA Objects March 20, 2020 54 / 150


Integers in Smalltalk 80

In Magnitude
>= aMagnitude
↑ ( self < aMagnitude ) not
In Date
< aDate
year < aDate year
ifTrue : [ ↑ day < aDate day ]
ifFalse : [ ↑ year < aDate year ]

TYLA Objects March 20, 2020 55 / 150


Collections in Smalltalk 80
Object

Collection

Bag Set

SequenceableCollection
Dictionary
LinkedList Interval

ArrayedCollection OrderedCollection

Array String Text SortedCollection

Symbol

TYLA Objects March 20, 2020 56 / 150


Collections in Smalltalk 80

In LinkedList:
do : aBlock
| aLink |
aLink <- firstLink .
[ aLink = nil ] whileFalse :
[ aBlock value : aLink .
aLink <- aLink nextLink ]

TYLA Objects March 20, 2020 57 / 150


Using Smalltalk 80 Collections

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 ]

TYLA Objects March 20, 2020 58 / 150


The Smalltalk 80 Environment

Everything is sorted, classified,


so that the programmers can browse the system.
Everything is object.
The system is reflexive.
The inspector to examine an object.
Coupled to the debugger and the interpretor,
a wonderful programming environment.
Big success of Smalltalk in prototyping.

TYLA Objects March 20, 2020 59 / 150


Sub-classing in Smalltalk 80: Complexes
Chose a superclass: Number.
Browse onto it (look in the Numeric-Numbers category). A
skeleton is proposed.
Number subclass : # Complex
instanceVariableNames : ’ ’
classVariableNames : ’ ’
poolDictionaries : ’ ’
category : ’ Numeric - Numbers ’
Complete.
Number subclass : # Complex
instanceVariableNames : ’ re im ’
classVariableNames : ’ ’
poolDictionaries : ’ ’
category : ’ Numeric - Numbers ’
TYLA Objects March 20, 2020 60 / 150
Sub-classing in Smalltalk 80: Complexes

Validate.
Go into the Complex class, class methods, and create:
re : realPart im : imPart
↑ ( self new ) setRe : realPart setIm : imPart

TYLA Objects March 20, 2020 61 / 150


Sub-classing in Smalltalk 80: Complexes
Instance methods:
setRe : realPart setIm : imPart
re <- realPart .
im <- 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

First solution: implement asComplex in Number and Complex


" Class Number : addition . "
+ aNumber
| c |
c <- aNumber asComplex .
↑ Complex re : ( re + c re ) im : ( im + c im )
Second solution: implement re and im in Number.
But these don’t address:
666 + ( Complex re : 42 im : 51)
This issue was known by Smalltalk designers who faced it for other
Number subclasses; they introduced the generality class method.

TYLA Objects March 20, 2020 63 / 150


Smalltalk 80 Criticism

Some loopholes in the semantics.


The metaclass concept was considered too difficult.
No typing!
Dynamic dispatch exclusively, that’s slow.
The GC is nice, but slow too.
The virtual image prevents collaborative development.
No security (one can change anything).
No means to produce standalone applications.
No multiple inheritance.

TYLA Objects March 20, 2020 64 / 150


Demo with Squeak

https://squeak.org
TYLA Objects March 20, 2020 65 / 150
Table of Contents

1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming

TYLA Objects March 20, 2020 66 / 150


Reading...

TYLA Objects March 20, 2020 67 / 150


What is this Book ?

TYLA Objects March 20, 2020 68 / 150


Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language


People behind Eiffel
Overview of the System
Overview of the Language
Handling Multiple inheritance
Programming ”by Contract” (applied to OOP)

4 C++

5 CLOS
TYLA Objects March 20, 2020 69 / 150
Bertrand Meyer (1950), MIT

TYLA Objects March 20, 2020 70 / 150


Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language


People behind Eiffel
Overview of the System
Overview of the Language
Handling Multiple inheritance
Programming ”by Contract” (applied to OOP)

4 C++

5 CLOS
TYLA Objects March 20, 2020 71 / 150
Introducing Eiffel

High-level language designed for Software Engineering, portable,


with an original and clear syntax
Modern conception of multiple class inheritance
High level tools and programmatic concepts (Virtual classes,
Generics, Exceptions, etc.)
Lot of standard libraries

TYLA Objects March 20, 2020 72 / 150


Libraries

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.

TYLA Objects March 20, 2020 73 / 150


An Eiffel Application

An Eiffel Application is called a system.

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

TYLA Objects March 20, 2020 74 / 150


Clusters

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

TYLA Objects March 20, 2020 75 / 150


LACE File Example

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

TYLA Objects March 20, 2020 76 / 150


Original Concepts

Adaptation clauses for inheritance


resolve multiple inheritance problems
Contract Programming
Promote reusability and modularity
Graphical User Interface
A full dedicated GUI: drag-and-drop, etc.

TYLA Objects March 20, 2020 77 / 150


A smart compiler

Compiler with three modes really usefull in developpement phases

TYLA Objects March 20, 2020 78 / 150


A smart compiler

Compiler with three modes really usefull in developpement phases

FINALIZING Optimisation and production of an executable file where


all optimizations have been applied. May be very slow!

TYLA Objects March 20, 2020 78 / 150


A smart compiler

Compiler with three modes really usefull in developpement phases

FINALIZING Optimisation and production of an executable file where


all optimizations have been applied. May be very slow!
FREEZING compile and produce an executable file

TYLA Objects March 20, 2020 78 / 150


A smart compiler

Compiler with three modes really usefull in developpement phases

FINALIZING Optimisation and production of an executable file where


all optimizations have been applied. May be very slow!
FREEZING compile and produce an executable file
MELTING compilation by patches. Very fast, a modification only
recompile what is necessary (not good performance,
useful for developpement)

TYLA Objects March 20, 2020 78 / 150


A full System

EiffelBench the visual workbench for object-oriented development

TYLA Objects March 20, 2020 79 / 150


A full System

EiffelBench the visual workbench for object-oriented development


EiffelBuild the editor to build GUI

TYLA Objects March 20, 2020 79 / 150


A full System

EiffelBench the visual workbench for object-oriented development


EiffelBuild the editor to build GUI
EiffelCase the tools dedicated to build and design application

TYLA Objects March 20, 2020 79 / 150


Ebench

TYLA Objects March 20, 2020 80 / 150


Edit a Class

TYLA Objects March 20, 2020 81 / 150


Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language


People behind Eiffel
Overview of the System
Overview of the Language
Handling Multiple inheritance
Programming ”by Contract” (applied to OOP)

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 ;

-- une m é thode : changer les coordonn é es


set_x_y (x , y : INTEGER ) is
do
xc := x ;
yc := y ;
end ;
end -- class POINT

TYLA Objects March 20, 2020 83 / 150


Methods Calls

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

TYLA Objects March 20, 2020 84 / 150


Creating an Object

variable of type reference


p : POINT ; p

do created object
...
!! p ; −− instruction
...
end ;

Except for explicit declaration, all the object’s variables are


references: they handle pointers.

TYLA Objects March 20, 2020 85 / 150


Creation with Initialization 1/2

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

TYLA Objects March 20, 2020 86 / 150


Creation with Initialization 2/2

The object can then be created using its initialization method


p : POINT ;
create p . make (23 ,64) ;; -- create and initialize a Point

Multiple initialization methods can be defined for a same class.


The correct method is chosen during the creation.

When (at least) one initialization method is declared for an class,


this class cannot be created without calling one of these routines.
⇒ Security

TYLA Objects March 20, 2020 87 / 150


Access to Class Member Variables

READING By default, all members are readable: everyone can


know the value of it (but restriction can be applied).
WRITTING Members are NEVER writtable except for the current
object. The object mus provide a setter!
set x y(x,y : INTEGER) de la classe POINT.
⇒ Security

TYLA Objects March 20, 2020 88 / 150


Access to Class Member Variables

READING By default, all members are readable: everyone can


know the value of it (but restriction can be applied).
WRITTING Members are NEVER writtable except for the current
object. The object mus provide a setter!
set x y(x,y : INTEGER) de la classe POINT.
⇒ Security

Method without arguments doesn’t have an empty pair of


parenthesis: this helps to keep API stable

TYLA Objects March 20, 2020 88 / 150


Expanded Class

By default, all types are manipulated by reference


(i.e., with indirection)

Performances issues . . .

expanded class ERECTANGLE


inherit
RECTANGLE ;
end -- class ERECTANGLE

TYLA Objects March 20, 2020 89 / 150


Like current
Let us consider the following snippet from class SHAPE:
copy_and_move (x , y : INTEGER ) : SHAPE is do
Result := Current . copy () ;
Result . set_x_y (x , y ) ;
end ;

And the following code :


C1 := C2 . copy_and_move (22 ,23)

No problem when C1 and C2 are SHAPE


Problem: type is lost when C1 and C2 are of type SQUARE
(that inherits from SHAPE)
=⇒ Effeil proposes like x or like current to solve this!

TYLA Objects March 20, 2020 90 / 150


Eiffel Overview
An object-oriented program structure in which a class serves as
the basic unit of decomposition
Static Typing
Protection against calls on null references, through the
attached-types mechanism
Objects that wrap computations (closely connected with closures
and lambda calculus)
Garbage Collection
Simple Concurrent Object-Oriented Programming
Constrained and unconstrained generic programming in a latter
lecture
Design by contract
Fine grained (multiple) inheritance handling
TYLA Objects March 20, 2020 91 / 150
Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language


People behind Eiffel
Overview of the System
Overview of the Language
Handling Multiple inheritance
Programming ”by Contract” (applied to OOP)

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.

TYLA Objects March 20, 2020 93 / 150


Problem Statement

Class A Class B
Simple Inheritance: a primitive primitive
class may herit at most from p p

only one class


Multiple Inheritance: What does
more powerful than the Class C
X.p
simple inheritance but inherit
A mean ?
introduces problems. B
Eiffel proposes the
adaptation clauses to solve X
these problems.

TYLA Objects March 20, 2020 93 / 150


Multiple Inheritance is Sometimes Necessary

SHAPE

POLYGON

SQUARE COLORED_SHAPE

COLORED_SQUARE

TYLA Objects March 20, 2020 94 / 150


Inheritance for factorization
Simple inheritance helps to factorization:
AB
A B
A B

And multiple inheritance is sometimes mandatory


AB BC

A B C A B C

Smalltalk, Java, ... only propose a solution for modelisation while


Eiffel also solves the factorization problems.

TYLA Objects March 20, 2020 95 / 150


Jointure of primitives

Two corner cases :


class A
p

class B class C class B class C

deferred p deferred p

class D class D

deferred is an Eiffel keyword meaning virtual in C++


TYLA Objects March 20, 2020 96 / 150
Quick Overview of the Other Languages

Multiple inheritance is forbidden because it raises numerous


problems and it is not necessary.
⇒ Java, Smalltalk, Ada
Chosse a lookup strategy and the programmer must conform it:
⇒ C++
Propose tools (in the language) for solving problems related to
multiple inheritance
⇒ Eiffel’s inheritance adaptation clauses.

TYLA Objects March 20, 2020 97 / 150


Adaptation Clauses

Features:
Rename inherited primitives

TYLA Objects March 20, 2020 98 / 150


Adaptation Clauses

Features:
Rename inherited primitives
Modify Visibility of inherited primitives

TYLA Objects March 20, 2020 98 / 150


Adaptation Clauses

Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)

TYLA Objects March 20, 2020 98 / 150


Adaptation Clauses

Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Redefine inherited primitives

TYLA Objects March 20, 2020 98 / 150


Adaptation Clauses

Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Redefine inherited primitives
Selection clauses

TYLA Objects March 20, 2020 98 / 150


Adaptation Clauses

Features:
Rename inherited primitives
Modify Visibility of inherited primitives
A-definition inherited primitives (make a primitive virtual)
Redefine inherited primitives
Selection clauses

With these operations, we can resolve all


problems related to multiple inheritance.

TYLA Objects March 20, 2020 98 / 150


Renaming Clauses
class SQUARE

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 ;

end -- class SQUARE

TYLA Objects March 20, 2020 99 / 150


(French) Example
TELEPHONE OBJET_MURAL

méthode méthode

décrocher() décrocher()

rename
decrocher
as
TELEPHONE_MURAL decrocher_du_mur

TYLA Objects March 20, 2020 100 / 150


Visibility Filter
class SQUARE

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 )

TYLA Objects March 20, 2020 102 / 150


Redefinition Clauses
class SQUARE

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

TYLA Objects March 20, 2020 103 / 150


Keep and redefine
Redefinition to support dynamic lookup

Here, we loose dynamic lookup


class B

inherit
A
rename p as pa end ;

feature

p ... is ...

TYLA Objects March 20, 2020 104 / 150


Keep and redefine
Redefinition to support dynamic lookup
Here, dynamic lookup will work:
Here, we loose dynamic lookup
class B
class B
inherit
inherit A
A rename p as pa end ;
rename p as pa end ; A
redefine p end ;
feature
feature
p ... is ...
p ... is ...

TYLA Objects March 20, 2020 104 / 150


Selection Clauses

How to resolve this problem:


class A
Given x : A, what does x.p
primitive p
means? If x references an instance
primitive q
of class A, it is the primitive p
redefine p redefine p from A. Same thing happens for
class B class C
an object of B ou C. What about
primitive p primitive p
instances of class D ?
rename p as pb rename p as pc Example:
class D

q () is do p () end ;

TYLA Objects March 20, 2020 105 / 150


A-definition

The A-definition allows to undefine methods


Useful to ”delete” methods that don’t make sense anymore.

class TELEPHONE_MURAL
inherit
TELEPHONE ;
OBJET_MURAL
undefine decrocher
end ;
...

TYLA Objects March 20, 2020 106 / 150


Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language


People behind Eiffel
Overview of the System
Overview of the Language
Handling Multiple inheritance
Programming ”by Contract” (applied to OOP)

4 C++

5 CLOS
TYLA Objects March 20, 2020 107 / 150
What is that?

”It is absurd to make elaborate security checks on debugging runs,


when no trust is put in the results, and then remove them in
production runs, when an erroneous result could be expensive or
disastrous. What would we think of a sailing enthusiast who wears
his life-jacket when training on dry land but takes it off as soon as he
goes to sea?”

Charles Antony Richard Hoare

TYLA Objects March 20, 2020 108 / 150


Goals

In everyday life a service or a product typically comes with a contract


or warranty: an agreement in which one party promises to
supply the service or product for the benefit of some other
party.

An effective contract for a service specifies requirements:


Conditions that the consumer must meet in order for the service
to be performed
⇒ Preconditions
Condition that the provider must meet in order for the service to
be acceptable
⇒ Postconditions

TYLA Objects March 20, 2020 109 / 150


Some History

Has roots in work on formal verification, formal specification and


Hoare logic
First introducted by Eiffel
Supported natively by Ada (2012), D, C#
Librairies to emulate it in Java (cofoja), Javascript (contract.js),
Python (pycontracts), C++ (Boost) . . .

TYLA Objects March 20, 2020 110 / 150


Contracts
A lot of ontracts:
Pre-conditions and postconditions of a method
Class invariants
Assertions
Loop invariants

TYLA Objects March 20, 2020 111 / 150


Contracts
A lot of ontracts:
Pre-conditions and postconditions of a method
Class invariants
Assertions
Loop invariants

Contracts are part of the language:


a dedicated syntaxe
compiled (or not) according to the given options
used by the compiler
used by the environnemnt
used by the documentation

TYLA Objects March 20, 2020 111 / 150


Pre-conditions
Pre-conditions must be fulfill by the client, i.e. based on arguments

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

TYLA Objects March 20, 2020 112 / 150


Post-conditions
Post-conditions must be fulfill by the provider, i.e. if the client fulfill
preconditions, the provider will fulfill postcondiitons.

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

Referencing previous value in Post-conditions (Eiffel)


TYLA Objects March 20, 2020 114 / 150
Stripping Objects
In a postcondition, strip(x,y,..) references an object where all
attributes x and y , ... have been removed

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 ;

Stripping Object in Postconditions (Eiffel)


TYLA Objects March 20, 2020 115 / 150
Redefinition (1/2)

class A

routine p is require ... ensure ... end ;

routine q is do p() ; end ;


The redefined method p in B can
be used instead of the original
redefine p
method p de A.
class B ⇒ Assertions are inherited
routine p is do ... end ;

TYLA Objects March 20, 2020 116 / 150


Redefinition (2/2)

The redefined method must


satisfy old assertions but
can be more precise:

TYLA Objects March 20, 2020 117 / 150


Redefinition (2/2)

The redefined method must


satisfy old assertions but
can be more precise:
Release some
preconditions

TYLA Objects March 20, 2020 117 / 150


Redefinition (2/2)

The redefined method must


satisfy old assertions but
can be more precise:
Release some
preconditions
Add (Restrict)
postconditions

TYLA Objects March 20, 2020 117 / 150


Redefinition (2/2)

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

TYLA Objects March 20, 2020 117 / 150


Class Invariants
A Class Invariant is an assertion attached to an object. The inherited
class also inherits invariants.
class RECTANGLE
...

invariant
( xc < 0 implies width > - xc ) -- visible
and
( yc < 0 implies height > - yy ) -- visible
and
width >= 0
and
height >= 0

end -- class RECTANGLE

TYLA Objects March 20, 2020 118 / 150


Assertions

Can be inserted anywhere in the code.

-- Code
check
x > 0 ;
y < 0 implies largeur > -y
end ;

TYLA Objects March 20, 2020 119 / 150


Loop (in)variants
Only one (complex) kind of loop in Eiffel
from
-- initialization
...
invariant
-- checked each iteration
...
variant
-- positive integer expression
...
until
-- exit condition
...
loop
-- loop body
...
end ;

TYLA Objects March 20, 2020 120 / 150


Table of Contents

1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming

TYLA Objects March 20, 2020 121 / 150


Bjarne Stroustrup

TYLA Objects March 20, 2020 122 / 150


Bjarne Stroustrup

TYLA Objects March 20, 2020 123 / 150


Bjarne Stroustrup

TYLA Objects March 20, 2020 124 / 150


Bjarne Stroustrup

TYLA Objects March 20, 2020 125 / 150


Bjarne Stroustrup

TYLA Objects March 20, 2020 126 / 150


C++

Bjarne Stroustrup,
BellLabs, 1982.
cfront, a C preprocessor.
G++, the first real C++ compiler.
Standardized in 1998.

TYLA Objects March 20, 2020 127 / 150


A better and safer C

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.

TYLA Objects March 20, 2020 128 / 150


Poor Error Messages

# include < iostream >


# include < list >
int
main () {
std :: list < int > list ;
list . push_back (1);
list . push_back (2);
list . push_back (3);
const std :: list < int > list2 = list ;
for ( std :: list < int >:: iterator i = list2 . begin ();
i != list2 . end (); ++ i )
std :: cout << * i << std :: endl ;
}

TYLA Objects March 20, 2020 129 / 150


Poor Error Messages – G++ 2.95

bar . cc : In function i n t main () :


bar . cc :13: conversion from
_ L i s t _ i t e r a t o r < int , const int & , const int * >
to non - scalar type
_ L i s t _ i t e r a t o r < int , int & , int * > requested
bar . cc :14: no match for
_ L i s t _ i t e r a t o r < int , int & , int * > & !=
_List_iterator < int , const int & , const int * >
/ usr / lib / gcc - lib / i386 - linux / 2 . 9 5 . 4 / . . / . . / . . / . . / include / g
candidates are :
bool _List_iterator < int , int & , int * >:: operator !=
( const _List_iterator < int , int & , int * > &) const

TYLA Objects March 20, 2020 130 / 150


Table of Contents

1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming

TYLA Objects March 20, 2020 131 / 150


CLOS

Developed in mid 80’s.

Overview:
Metaobject Protocol
Meta-class
Multiple Inheritance
Multiple dispatch
Generic Functions
Method Qualifier
Introspection

TYLA Objects March 20, 2020 132 / 150


Small Example
( defclass human () ( name size birth-year ))
( make-instance ’ human )

( defclass Shape () ())


( defclass Rectangle ( Shape ) ())
( defclass Ellipse ( Shape ) ())
( defclass Triangle ( Shape ) ())

( defmethod intersect (( r Rectangle ) ( e Ellipse ))


...)

( defmethod intersect (( r1 Rectangle ) ( r2 Rectangle ))


...)

( defmethod intersect (( r Rectangle ) ( s Shape ))


...)

TYLA Objects March 20, 2020 133 / 150


Luca Cardelli

TYLA Objects March 20, 2020 134 / 150


Engineering Properties, 1996

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?

TYLA Objects March 20, 2020 135 / 150


Economy of execution

Type information was first introduced in programming to improve


code generation and run-time efficiency for numerical computations.
In ML, accurate type information eliminates the need for nil-checking
on pointer dereferencing.
Object-oriented style intrinsically less efficient that procedural style
(virtual). The traditional solution to this problem (analyzing and
compiling whole programs) violates modularity and is not applicable
to libraries.
Much can be done to improve the efficiency of method invocation by
clever program analysis, as well as by language features (e.g. final).
Design type systems that can statically check many of the conditions
that now require dynamic subclass checks.

TYLA Objects March 20, 2020 136 / 150


Economy of compilation

Type information can be organized into interfaces for program


modules (Modula-2, Ada...). Modules can then be compiled
independently. Compilation of large systems is made more efficient.
The messy aspects of system integration are thus eliminated.
Often, no distinction between the code and the interface of a class.
Some object-oriented languages are not sufficiently modular and
require recompilation of superclasses when compiling subclasses.
Time spent in compilation may grow disproportionally with the size
of the system.
We need to adopt languages and type systems that allow the separate
compilation of (sub)classes, without resorting to recompilation of
superclasses and without relying on private information in interfaces.

TYLA Objects March 20, 2020 137 / 150


Economy of small-scale development

Well designed type systems allow typechecking to capture a large


fraction of routine programming errors. Remaining errors are easier to
debug: large classes of other errors have been ruled out. Typechecker
as a development tool (changing the name of a type when its
invariants change even though the type structure remains the same).
Big win of OO: class libraries and frameworks. But when ambition
grows, programmers need to understand the details of those class
libraries: more difficult than understanding module libraries. The type
systems of most OOL are not expressive enough; programmers must
often resort to dynamic checking or to unsafe features, damaging the
robustness of their programs. Improvements in type systems for OOL
will improve error detection and the expressiveness of interfaces.

TYLA Objects March 20, 2020 138 / 150


Economy of large-scale development

Data abstraction and modularization have methodological advantages


for development. Negotiate the interfaces, then proceed separately.
Polymorphism is important for reusing code modularly.
Teams developing/specializing class libraries. Reuse is a big win of
OOL, but poor modularity wrt class extension and modification
(method removal, etc.). Confusion bw classes and object types
(limits abstractions). Subtype polymorphism is not good enough for
containers. Formulating and enforcing inheritance interfaces: the
contract bw a class and its subclasses. Requires language support
development. Parametric polymorphism is beginning to appear but
its interactions with OO features need to be better understood.
Interfaces/subtyping and classes/subclassing must be separated.

TYLA Objects March 20, 2020 139 / 150


Economy of language features
Well-designed orthogonal constructs can be naturally composed
(array of arrays; n-ary functions vs 1-ary and tuples). Orthogonality
reduces the complexity of languages. Learning curve thus reduced,
re-learning minimized.
Smalltalk, good. C++ daunting in the complexity of its many
features. Somewhere something went wrong; what started as
economical and uniform (everything is an object) ended up as a
baroque collection of class varieties. Java represents a healthy
reaction, but is more complex than many people realize.
Prototype-based languages tried to reduce the complexity by
providing simpler, more composable features, but much remains to be
done for class-based languages. How can we design an OOL that
allows powerful engineering but also simple and reliable engineering?

TYLA Objects March 20, 2020 140 / 150


Table of Contents

1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming

TYLA Objects March 20, 2020 141 / 150


Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language

4 C++

5 CLOS

6 Prototype-based programming
Self
Heirs
TYLA Objects March 20, 2020 142 / 150
Problem Statement

Traditional class-based OO languages are based on a deep-rooted


duality:
Classes: defines behaviours of objects.
Object instances: specific manifestations of a class

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

TYLA Objects March 20, 2020 143 / 150


Self

Invented by David Ungar and Randall B. Smith in 1986 at Xerox Park

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

TYLA Objects March 20, 2020 144 / 150


Example in self

Copy object lecture and set fill title to TYLA


tyla := lecture copy title : ’ TYLA ’.

add slot to an object


tyla _AddSlots : (| remote <- ’ true ’ |).

Modifies at runtime the parent


myObject parent : someOtherObject .

TYLA Objects March 20, 2020 145 / 150


Impacts

Javascript

NewtonScript

Io

Rust

Go

TYLA Objects March 20, 2020 146 / 150


Table of Contents
1 Simula

2 Smalltalk

3 The mysterious language

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.

TYLA Objects March 20, 2020 148 / 150


Example in Rust
trait Foo {
fn method (& self ) -> String ;
}

impl Foo for u8 {


fn method (& self ) -> String
{ format !( " u8 : {} " , * self ) }
}

impl Foo for String {


fn method (& self ) -> String
{ format !( " string : {} " , * self ) }
}

fn do_something < T : Foo >( x : T ) {


x . method ();
}

TYLA Objects March 20, 2020 149 / 150


Duck Typing

If it walks like a duck and it quacks like a duck,


then it must be a duck

Deffered to a later lecture (about Genericity)

TYLA Objects March 20, 2020 150 / 150

You might also like