Object Inheritance Without Classes Timothy Jones , Michael Homer, - - PowerPoint PPT Presentation

object inheritance without classes
SMART_READER_LITE
LIVE PREVIEW

Object Inheritance Without Classes Timothy Jones , Michael Homer, - - PowerPoint PPT Presentation

Object Inheritance Without Classes Timothy Jones , Michael Homer, James Noble Victoria University of Wellington {tim,mwh,kjx}@ecs.vuw.ac.nz Kim Bruce Pomona College kim@cs.pomona.edu July 21, 2016 Foundations Objects v Classes 1


slide-1
SLIDE 1

Object Inheritance Without Classes

Timothy Jones, Michael Homer, James Noble Victoria University of Wellington {tim,mwh,kjx}@ecs.vuw.ac.nz Kim Bruce Pomona College kim@cs.pomona.edu July 21, 2016

slide-2
SLIDE 2

Foundations

Objects v Classes

1

slide-3
SLIDE 3

Foundations

Objects v Classes

Andrew v Kim

1

slide-4
SLIDE 4

Foundations

Objects v Classes

Andrew v Kim Objects-first v Objectdraw

1

slide-5
SLIDE 5

Foundations

Know Thy Self

This problem is solved! ( | parent* = other. | )

2

slide-6
SLIDE 6

Foundations

Know Thy Self

This problem is solved! ( | parent* = other. | ) ( | parent* = factory new. | )

2

slide-7
SLIDE 7

Foundations

Know Thy Self

This problem was supposed to be solved. . . ( | parent* = other. | ) ( | parent* = factory new. | )

2

slide-8
SLIDE 8

Semantics

Object Inheritance

method graphic(canvas) {

  • bject {

. . . } } def amelia = object { inherit graphic(canvas) . . . }

3

slide-9
SLIDE 9

Semantics

Semantics

What does this mean? inherit graphic(canvas) Do the inherit semantics actually allow us to implement classes?

◮ Let’s investigate different object inheritance semanticses

4

slide-10
SLIDE 10

Semantics

Semantics

5

slide-11
SLIDE 11

Semantics

Semantics

6

slide-12
SLIDE 12

Semantics

Semantics

7

slide-13
SLIDE 13

Semantics

Semantics

8

slide-14
SLIDE 14

Semantics

Semantics

9

slide-15
SLIDE 15

Semantics

Implementation

Runnable semantics with PLT Redex https://github.com/zmthy/graceless-redex

10

slide-16
SLIDE 16

Semantics

Reg. Down. Dist. Stable Exist. Mult. Forwarding Delegation Concatenation Merged Uniform

  • Mult. Uniform

Transform U. Positional U. Java yes yes no yes class no (* indicates true for construction, then reversed afterwards)

11

slide-17
SLIDE 17

Object Inheritance

Object Inheritance

Objects inherit directly from one another Three foundational models:

◮ Forwarding (as in E) ◮ Delegation (as in JavaScript and Self) ◮ Concatenation (as in Kevo)

12

slide-18
SLIDE 18

Object Inheritance

Forwarding

Requests to inherited methods go directly to inherited object

◮ Simplest semantics

a method m(x) b

13

slide-19
SLIDE 19

Object Inheritance

Forwarding

Requests to inherited methods go directly to inherited object

◮ Simplest semantics

a method m(x) b m(v)

13

slide-20
SLIDE 20

Object Inheritance

Forwarding

Requests to inherited methods go directly to inherited object

◮ Simplest semantics

a method m(x) b m(v) m(v)

13

slide-21
SLIDE 21

Object Inheritance

Forwarding

Requests to inherited methods go directly to inherited object

◮ Simplest semantics

a method m(x) b m(v) m(v) No down-calls (cannot modify existing implementation)

13

slide-22
SLIDE 22

Object Inheritance

Down-calls

method graphic(canvas) {

  • bject {

method image { abstract } method draw { canvas.render( image ) } } } def amelia = object { inherit graphic(canvas) def image = images.amelia }

14

slide-23
SLIDE 23

Object Inheritance

Delegation

Requests to inherited methods have self bound to original object

◮ The standard semantics of object inheritance

a method m(x){ self.k(x) } b

15

slide-24
SLIDE 24

Object Inheritance

Delegation

Requests to inherited methods have self bound to original object

◮ The standard semantics of object inheritance

a method m(x){ self.k(x) } b m(v)

15

slide-25
SLIDE 25

Object Inheritance

Delegation

Requests to inherited methods have self bound to original object

◮ The standard semantics of object inheritance

a method m(x){ self.k(x) } b m(v) m(v)

15

slide-26
SLIDE 26

Object Inheritance

Delegation

Requests to inherited methods have self bound to original object

◮ The standard semantics of object inheritance

a method m(x){ self.k(x) } b m(v) m(v) k(v)

15

slide-27
SLIDE 27

Object Inheritance

Delegation

Requests to inherited methods have self bound to original object

◮ The standard semantics of object inheritance

a method m(x){ self.k(x) } b m(v) m(v) k(v) Vampire problem

15

slide-28
SLIDE 28

Object Inheritance

Delegation

Requests to inherited methods have self bound to original object

◮ The standard semantics of object inheritance

a method m(x){ self.k(x) } b m(v) m(v) k(v) Vampire problem Surprising behaviour if you’re used to classes

15

slide-29
SLIDE 29

Object Inheritance

Action at a Distance

method graphic(canvas) {

  • bject {

var name := "A graphic" } } def parent = graphic(canvas) def amelia = object { inherit parent name := "Amelia" }

16

slide-30
SLIDE 30

Object Inheritance

Delegation (as in Self)

above = (| value ← 3. run = (|| say). say = (|| ’above’ printLine) |). below = (| parent* = above. say = (|| ’below’ printLine) | run. value: 5).

  • ther = (|

parent* = above. | value print).

17

slide-31
SLIDE 31

Object Inheritance

Concatenation

Copy the methods and fields from the inherited object

◮ Removes direct relationship between inheritor and inheritee

a method m(x) method k(y)

18

slide-32
SLIDE 32

Object Inheritance

Concatenation

Copy the methods and fields from the inherited object

◮ Removes direct relationship between inheritor and inheritee

a method m(x) b method m(x); method k(y)

18

slide-33
SLIDE 33

Object Inheritance

Concatenation

Copy the methods and fields from the inherited object

◮ Removes direct relationship between inheritor and inheritee

a method m(x) b method m(x); method k(y) Changes to inherited object are not reflected in inheriting object

18

slide-34
SLIDE 34

Object Inheritance

Registration

method graphic(canvas) {

  • bject {

canvas.register( self ) } } def amelia = object { inherit graphic(canvas) }

19

slide-35
SLIDE 35

Emulating Classes

Emulating Classes

Objects inherit from calls to constructor methods Two class-like models

◮ Merged Identity (as in C++) ◮ Uniform Identity (as in Java)

Cannot inherit from preëxisting objects

20

slide-36
SLIDE 36

Emulating Classes

Merged Identity

Inheriting object ‘becomes’ the inherited object

◮ Registered identities eventually resolve to the intended object

a method m(x) method k(y)

21

slide-37
SLIDE 37

Emulating Classes

Merged Identity

Inheriting object ‘becomes’ the inherited object

◮ Registered identities eventually resolve to the intended object

a method m(x); method k(y)

21

slide-38
SLIDE 38

Emulating Classes

Merged Identity

Inheriting object ‘becomes’ the inherited object

◮ Registered identities eventually resolve to the intended object

a method m(x); method k(y) Body-snatchers problem

21

slide-39
SLIDE 39

Emulating Classes

Merged Identity

Inheriting object ‘becomes’ the inherited object

◮ Registered identities eventually resolve to the intended object

a method m(x); method k(y) Body-snatchers problem Objects not stable during construction

21

slide-40
SLIDE 40

Emulating Classes

Stability

method graphic(canvas) {

  • bject {

image method image { abstract } } } def amelia = object { inherit graphic(canvas) def image = images.amelia }

22

slide-41
SLIDE 41

Emulating Classes

Uniform Identity

Inherited initialisation code runs as the inheriting object

◮ Basically magic

method m(x) b method k(y)

23

slide-42
SLIDE 42

Emulating Classes

Uniform Identity

Inherited initialisation code runs as the inheriting object

◮ Basically magic

b method m(x); method k(y)

23

slide-43
SLIDE 43

Emulating Classes

Uniform Identity

Inherited initialisation code runs as the inheriting object

◮ Basically magic

b method m(x); method k(y)

23

slide-44
SLIDE 44

Emulating Classes

Uniform Identity

Inherited initialisation code runs as the inheriting object

◮ Basically magic

b method m(x); method k(y) Uninitialised state during construction

23

slide-45
SLIDE 45

Emulating Classes

Emulating Classes

Not very satisfactory as foundational models

◮ No inheritance from preëxisting objects

24

slide-46
SLIDE 46

Emulating Classes

Emulating Classes

Not very satisfactory as foundational models

◮ No inheritance from preëxisting objects

Other languages (JavaScript, E) achieve this using other features

24

slide-47
SLIDE 47

Emulating Classes

Classes in JavaScript

function Above() { this.value = 3; this.say(); } Above.prototype.run = function () { this.say(); }; function Below() { Above.call(this); } Below.prototype.say = function () { console.log("hello"); }; new Below().run();

25

slide-48
SLIDE 48

Emulating Classes

Classes in E

def makeAbove(self) { def above { to run() { self.say() } } self ← say() return above } def below extends makeAbove(below) { to say() { println("hello") } } below.run()

26

slide-49
SLIDE 49

Conclusion

Multiple Inheritance

Every model except merged identity Various different conflict resolution schemes

◮ Named supers ◮ Method transformations ◮ Positional inheritance

27

slide-50
SLIDE 50

Conclusion

Reg. Down. Dist. Stable Exist. Mult. Forwarding no no yes yes yes can Delegation no no* yes no yes can Concatenation no no* no no yes can Merged yes no* no no* fresh can’t Uniform yes yes no yes fresh no

  • Mult. Uniform

yes yes no yes fresh yes Transform U. yes yes no no fresh yes Positional U. yes yes no no fresh yes Java yes yes no yes class no (* indicates true for construction, then reversed afterwards)

28

slide-51
SLIDE 51

Conclusion

Reg. Down. Dist. Stable Exist. Mult. Forwarding no no yes yes yes can Delegation no no* yes no yes can Concatenation no no* no no yes can Merged yes no* no no* fresh can’t Uniform yes yes no yes fresh no

  • Mult. Uniform

yes yes no yes fresh yes Transform U. yes yes no no fresh yes Positional U. yes yes no no fresh yes Java yes yes no yes class no (* indicates true for construction, then reversed afterwards)

29

slide-52
SLIDE 52

Conclusion

Conclusion

No obviously superior semantics for object inheritance Emulating classes requires magic or complicated language features Ultimately depends on the design goals for the language

30

slide-53
SLIDE 53

Conclusion

Lessons

OO language designers

◮ Simple foundations do not imply simple design

31

slide-54
SLIDE 54

Conclusion

Lessons

OO language designers

◮ Simple foundations do not imply simple design

Everyone else

◮ Problems are hidden in solved designs

31

slide-55
SLIDE 55

Extra Slides

Semantics

32

slide-56
SLIDE 56

Extra Slides

Forwarding (as in E)

def above { to run() { above.say() } to say() { println("above") } }

33

slide-57
SLIDE 57

Extra Slides

Forwarding (as in E)

def above { to run() { above.say() } to say() { println("above") } } def below extends above { to say() { println("below") } } below.run()

33

slide-58
SLIDE 58

Extra Slides

Delegation (as in Self)

above = (| value ← 3. run = (|| say). say = (|| ’above’ printLine) |). below = (| parent* = above. say = (|| ’below’ printLine) | run).

34

slide-59
SLIDE 59

Extra Slides

Delegation (as in Self)

above = (| value ← 3. run = (|| say). say = (|| ’above’ printLine) |). below = (| parent* = above. say = (|| ’below’ printLine) | value: 5).

  • ther = (|

parent* = above. | value print).

34

slide-60
SLIDE 60

Extra Slides

Delegation (as in JavaScript-ish)

let above = {}; above.value = 3; above.run = function () { this.say(); }; above.say = function () { console.log("above"); }; let below = Object.create(above); below.say = function () { console.log("below"); }; below.value = 5; below.run(); console.log(above.value = below.value);

35