the power of simplicityclasses prototypes can modify methods only by subclassing objects can have...

23
the power of simplicity SELF Rolph Recto + Jonathan DiLorenzo Great Works in PL April 30, 2019

Upload: others

Post on 30-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

the power of simplicitySELF

Rolph Recto + Jonathan DiLorenzo

Great Works in PL

April 30, 2019

Page 2: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

2

SELF: The Power of SimplicityDavid Ungar, StanfordRandall B. Smith, Xerox PARC

OOPSLA 87

Page 3: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

3

Smalltalk-80

Simula67

Self

Javascript

Java

C++

Dahl and Nygaard

Kay, Ingalls, and Goldberg

Stroustrup

Ungar and Smith

Eich

Gosling, Sheridan, and Naughton

1995

1991

1987

1985

1980

1967

Page 4: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

4

Is JavaScript popular? It’s hard to say. Some Ajax developers profess (and demonstrate) love for it. Yet many curse it, including me. I still think of it as a quickie love-child of C and Self.

Brendan Eichhttps://brendaneich.com/2008/04/popularity/

Page 5: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

5

everything is an object

all interactions are message

passing

prototypes, not classes

principles of Self

Page 6: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

6

Smalltalk

everything isan object

methods and closures

controlstructures

primitive values

classes

Page 7: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

((4 fac) between: 10 And: 100) ifTrue: “Hi!” False: ”Bye!”

7

call “ifTrue:False:” on true with args “Hi!” and “Bye!”, return “Hi!”

call “fac” method on 4, return 24

call “between:And:” on 24 with args 10 and 100, return true

Page 8: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

8

Smalltalk Self

objects are instances of classes

objects are clonesof prototypes

Page 9: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

9

Smalltalk Self

objects are instances of classes

objects are clonesof prototypes

C++

Java Javascript

Page 10: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

10

classes prototypes

can modify methods only by subclassing

objects can have unique methods and fields

create objects by calling class constructor

create objects by cloning prototype

classes need metaclasses, etc. (infinite regress!)

no classes,no infinite regress

Page 11: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

11

classes p := (Point new) x: 7 y: 9p print

follow p’s class pointer, check if print is defined there

not defined there, so follow superclass pointer

found “print” in Object class! Invoke with receiver “p”

Page 12: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

12

classes p := (Point new) x: 1 y: 10p print

follow p’s class pointer, check if print is defined there

not defined there, so follow superclass pointer

found “print” in Object class! Invoke with receiver “p”

to have different print method, need to create Point subclass

Page 13: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

13

prototypes p:= (point clone) x: 7 y: 9p print

does p have print method? no, so follow parent pointer to delegate

does Point delegate have “print”? no, so follow parent pointer to delegate

does Object delegate have print? yes, invoke with “p” as receiver

Page 14: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

14

prototypes p:= (point clone) x: 1 y: 10p print

does p have print method? no, so follow parent pointer to delegate

does Point delegate have “print”? no, so follow parent pointer to delegate

does Object delegate have print? yes, invoke with “p” as receiver

to have special print method for p, define new slot in p -- no subclass needed!

Page 15: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

method invocation clones prototype activation record

15

activation as cloning

Page 16: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

16

state as behavior

field access and assignment are

messages to current receiver (self)

Page 17: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

17

state as behavior

p x // p.x

p x: 2 // p.x = 2

Page 18: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

18

example: points

traits

prototypes

root

_AddSlotsIfAbsent: (| traits = (). prototypes = ().|)

Page 19: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

19

example: points

traits

prototypes

root

cloneablecopy _Clone

traits _AddSlotsIfAbsent:(|cloneable=()|)traits cloneable _Define:(| copy = (_Clone).|)

Page 20: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

20

example: points

traits

prototypes

root

cloneable

point

copy _Clone

parent*

printString [method]

+ [method]

- [method]

traits _AddSlotsIfAbsent: (|point=()|)traits point _Define:(| parent* = traits cloneable. printString = … + aPoint = … - aPoint = …|)

Page 21: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

21

example: points

traits

prototypes

root

cloneable

pointpoint

copy _Clone

parent*

printString [method]

+ [method]

- [method]

parent*

x 0

x: ⇐

y 0

y: ⇐

prototypes _AddSlotsIfAbsent (|point=()|)prototypes point _Define:(| parent* = traits point. x <- 0. y <- 0.|)

Page 22: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

22

example: points

traits

prototypes

root

cloneable

pointpoint

copy _Clone

parent*

printString [method]

+ [method]

- [method]

((prototypes point) copy) x: 3 y: 4

parent*

x 3

x: ⇐

y 4

y: ⇐

parent*

x 0

x: ⇐

y 0

y: ⇐

Page 23: the power of simplicityclasses prototypes can modify methods only by subclassing objects can have unique methods and fields create objects by calling class constructor create objects

23

discussion

is Self a good influence on modern languages?

are there cases when simplicity should be

abandoned?

what are the tradeoffs of Self’s flexibility?