object inheritance without classes
play

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


  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

  2. Foundations Objects v Classes 1

  3. Foundations Objects v Classes Andrew v Kim 1

  4. Foundations Objects v Classes Andrew v Kim Objects-first v Objectdraw 1

  5. Foundations Know Thy Self This problem is solved! ( | parent* = other. | ) 2

  6. Foundations Know Thy Self This problem is solved! ( | parent* = other. | ) ( | parent* = factory new. | ) 2

  7. Foundations Know Thy Self This problem was supposed to be solved. . . ( | parent* = other. | ) ( | parent* = factory new. | ) 2

  8. Semantics Object Inheritance method graphic(canvas) { def amelia = object { object { inherit graphic(canvas) . . . . . . } } } 3

  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

  10. Semantics Semantics 5

  11. Semantics Semantics 6

  12. Semantics Semantics 7

  13. Semantics Semantics 8

  14. Semantics Semantics 9

  15. Semantics Implementation Runnable semantics with PLT Redex https://github.com/zmthy/graceless-redex 10

  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

  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

  18. Object Inheritance Forwarding Requests to inherited methods go directly to inherited object ◮ Simplest semantics a method m(x) b 13

  19. Object Inheritance Forwarding Requests to inherited methods go directly to inherited object ◮ Simplest semantics a method m(x) b m( v ) 13

  20. Object Inheritance Forwarding Requests to inherited methods go directly to inherited object ◮ Simplest semantics a method m(x) m( v ) b m( v ) 13

  21. Object Inheritance Forwarding Requests to inherited methods go directly to inherited object ◮ Simplest semantics a method m(x) m( v ) b m( v ) No down-calls (cannot modify existing implementation) 13

  22. Object Inheritance Down-calls method graphic(canvas) { object { method image { abstract } def amelia = object { method draw { inherit graphic(canvas) canvas.render( image ) def image = images.amelia } } } } 14

  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

  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

  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) } m( v ) b m( v ) 15

  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) } k( v ) m( v ) b m( v ) 15

  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) } k( v ) m( v ) b m( v ) Vampire problem 15

  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) } k( v ) m( v ) b m( v ) Vampire problem Surprising behaviour if you’re used to classes 15

  29. Object Inheritance Action at a Distance def parent = graphic(canvas) method graphic(canvas) { object { def amelia = object { var name := "A graphic" inherit parent } name := "Amelia" } } 16

  30. Object Inheritance Delegation (as in Self) above = (| value ← 3. run = (|| say). say = (|| ’above’ printLine) |). other = (| parent* = above. below = (| | value print). parent* = above. say = (|| ’below’ printLine) | run. value: 5). 17

  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

  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

  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

  34. Object Inheritance Registration method graphic(canvas) { object { def amelia = object { inherit graphic(canvas) canvas.register( self ) } } } 19

  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

  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

  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

  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

  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

  40. Emulating Classes Stability method graphic(canvas) { object { def amelia = object { image inherit graphic(canvas) def image = images.amelia method image { abstract } } } } 22

  41. Emulating Classes Uniform Identity Inherited initialisation code runs as the inheriting object ◮ Basically magic method m(x) b method k(y) 23

  42. Emulating Classes Uniform Identity Inherited initialisation code runs as the inheriting object ◮ Basically magic b method m(x) ; method k(y) 23

  43. Emulating Classes Uniform Identity Inherited initialisation code runs as the inheriting object ◮ Basically magic b method m(x) ; method k(y) 23

  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

  45. Emulating Classes Emulating Classes Not very satisfactory as foundational models ◮ No inheritance from preëxisting objects 24

  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

  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

  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

  49. Conclusion Multiple Inheritance Every model except merged identity Various different conflict resolution schemes ◮ Named supers ◮ Method transformations ◮ Positional inheritance 27

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend