Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

30
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and the dynamic model

description

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer. Lecture 12: More on references and the dynamic model. Purpose of this lecture. Few really new concepts, but gain a better understanding of the tricky matter of references - PowerPoint PPT Presentation

Transcript of Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

Page 1: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Einführung in die ProgrammierungIntroduction to Programming

Prof. Dr. Bertrand Meyer

Lecture 12: More on references and the dynamic model

Page 2: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

2

Purpose of this lectureFew really new concepts, but gain a better understanding of the tricky matter of references

Reminder on garbage collection and associated concepts

Page 3: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

3

AssignmentReplaces a value by another

x

y

2

0 p.set_coordinates (2, 1)

0

1

p

Page 4: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

4

Setting fields (in routines of the class)class

POSITIONfeature –– Access

x: REAL-- Horizontal position

y: REAL-- Vertical position

feature –– Element changeset_coordinates (xval, yval: REAL)

-- Set coordinates to (`xval', `yval').require

x_positive: xval >= 0y_positive: yval >= 0

do

ensurex_set: x = xvaly_set: y = yval

endend

x := xvaly := yval

Page 5: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

5

class LINKED_CELL feature item : INTEGER right : LINKED_CELL set_fields (n : INTEGER ; r : LINKED_CELL) -- Reset both fields. do item := n right := r endend

Effect of an assignmentReference types: reference assignmentExpanded types: value copy ite

mright

item := nright := r

t, u : LINKED_CELL

create t ; create u

t.set_fields (25, u)

3

See LINKABLE in EiffelBase

t 0

u

t.set_fields (25, u)

25

0

Page 6: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

6

A linked list of strings

Haldenegg

item right

Central

item right

Haupt-bahnhof

item right

(LINKABLE) (LINKABLE) (LINKABLE)

first_element last_element

active

count 3

Parade-platz

item right

: inserting at the end

(LINKABLE)

4

Page 7: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

7

Inserting an item at the endextend (v : STRING)

-- Add v to end.-- Do not move cursor.

localp : LINKABLE [STRING]

docreate p.make (v)if is_empty then

first_element := pactive := p

elselast_element.put_right ( p)if after then active := p end

endlast_element := pcount := count + 1

end

Page 8: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

8

Exercise (uses loops)Reverse a list!

Halden-eggitem right

Central

Haupt-bahnhof

last_element

first_element

count3

(LINKABLE)

(LINKED_LIST)

Parade-

platz

Page 9: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

9

Reversing a list

1 2 3 4 5

1 2 3 4 5

Page 10: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

10

Reversing a list

frompivot := first_elementfirst_element := Void

until pivot = Void loopi := first_elementfirst_element := pivotpivot := pivot.rightfirst_element.put_right (i )

end

first_element pivot

right

i

1 2 3 4 5

Page 11: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

11

Reversing a list

frompivot := first_elementfirst_element := Void

until pivot = Void loopi := first_elementfirst_element := pivotpivot := pivot.rightfirst_element.put_right (i )

end

first_element pivot

right

i

1 2 3 4 5

Page 12: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

12

Reversing a list

frompivot := first_elementfirst_element := Void

until pivot = Void loopi := first_elementfirst_element := pivotpivot := pivot.rightfirst_element.put_right (i )

end

first_element pivot

right

i

1 2 3 4 5

Page 13: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

13

Reversing a list

frompivot := first_elementfirst_element := Void

until pivot = Void loopi := first_elementfirst_element := pivotpivot := pivot.rightfirst_element.put_right (i )

end

first_element pivot

right

i

1 2 3 4 5

Page 14: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

14

ii pivotpivot

Reversing a list

frompivot := first_elementfirst_element := Void

until pivot = Void loopi := first_elementfirst_element := pivotpivot := pivot.rightfirst_element.put_right (i )

end

first_element

right

1 2 3 4 5

Page 15: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

15

The loop invariant

frompivot := first_elementfirst_element := Void

until pivot = Void loopi := first_elementfirst_element := pivotpivot := pivot.rightfirst_element.put_right (i )

end

first_element pivot

right

i

1 2 3 4 5

Invariant: from first_elementfollowing right, initial itemsin inverse order; from pivot,rest of items in original order

Page 16: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

16

The trouble with reference assignment

A comfortable mode of reasoning:

-- Here SOME_PROPERTY holds of a

“Apply SOME_OPERATION to b”

-- Here SOME_PROPERTY still holds of a

This applies to “expanded” values, e.g. integers

-- Here P (a) holds

OP (b)

-- Here P (a) still holds of a

Page 17: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

17

Dynamic aliasing

a, b: LINKABLE [STRING]

create a....

a.put ("Haldenegg")

b := a

-- Here a.item has value "Haldenegg"

b.put ("Paradeplatz")

-- Here a.item has value ?????

Haldenegg

item right

a b

Page 18: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

18

On the other hand...

-- I heard that the boss’s cousin earns less-- than 50,000 francs a year

“Raise Caroline’s salary by 1 franc”

-- ?????

Metaphors: “The beautiful daughter of Leda” “Menelas’s spouse” “Paris’s lover” = Helen of

Troy

Page 19: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

19

Practical advice

Reference assignment is useful

It’s also potentially tricky

As much as possible, leave it to specialized libraries of general data structures

Page 20: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

20

Variants of assignment and copy

Reference assignment (a and b of reference types): b := a

Object duplication (shallow): c := a.twin

Object duplication (deep): d := a.deep_twin

Also: shallow field-by-field copy (no new object is created):

e.copy (a)

Page 21: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

21

Shallow and deep cloningInitial situation:

Result of:b := a

c := a.twin

d := a.deep_twin

“Almaviva”namelandlord

loved_one

O1

“Figaro”O2 “Susanna”O3

b

“Almaviva”O4c

“Almaviva”O5

“Figaro”O6 “Susanna”O7

d

a

Page 22: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

22

Where do these mechanisms come from?

Class ANY in the Eiffel “Kernel Library”

Every class that doesn’t explicitly inherit from another is considered to inherit from ANY

As a result, every class is a descendant of ANY.

Page 23: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

23

Completing the inheritance structure

A B

D E

C

ANY

NONE

Inheritsfrom

Page 24: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

24

A related mechanism: Persistence

a.store (file)....b := retrieved (file)

Storage is automatic. Persistent objects identified individually by keys.

These features come from the library class STORABLE.

Needs to be improved,

see “ “object test”

Page 25: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

25

Objects and references

States of a reference:

Operations on references: create pp := qp := Voidif p = Void then ...

VOID ATTACHED

create pp := q (where q is attached)

p := Voidp := q (where q is void)

p ATTACHED

p VOID

Page 26: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

26

The object-oriented form of call

some_target.some_feature (some_arguments)

For example:

Paris.displayLine6.extend (Station_Parade_Platz)

x := a.plus (b) ???????

Page 27: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

27

Infix and prefix operators

In

a − bthe − operator is “infix” (written between operands)

In

− bthe − operator is “prefix” (written before the operand)

Page 28: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

28

Operator features

expanded class INTEGER feature

plus alias "+" (other : INTEGER): INTEGER-- Sum with other

do ... endtimes alias “*“ (other : INTEGER): INTEGER

-- Product by otherdo ... end

minus alias “-" : INTEGER-- Unary minus

do ... end...

end

Calls such as i.plus (j) can now be written i + j

Page 29: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

29

Reading assignment for next weekChapters on

Syntax (11) Inheritance (16)

Page 30: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

30

What we have seen

Playing with references: list reversalDynamic aliasing and the difficulties of

pointers & referencesOverall inheritance structureCopy, clone and storage operationsPersistence closureInfix & prefix operators