Plan
Pl
at semantics and p
principles
p
more carefully
y
Look at O-O and functional programming what
are the essential differences and similarities
L kU R
Look-Up
Rules
l (1)
Key idea in any language: how are
symbols
y
(names,
(
, identifiers)) resolved
Functional programming first-class
functions lexical scope
functions,
scope, immutability (i
(i.e.,
e
dont use set!)
L kU R
Look-Up
Rules
l iin R
Ruby
b (2)
fields (@
(@x),
) class fields (@@x)
(@@ ) vs
method/block variables and method names (x)
Fi t Cl
First-Class
Thi
Things
th
thatt are not:
t
Message names
cant write x.(if b then m else n end)
Blocks (but procs are)
Argument lists
V i bl L
Variable
Lookup
k iin R
Ruby
b
Else
M
Message
L
Lookup
k iin R
Ruby
b
If
Wh t is
What
i self?
lf?
Evaluation always takes place in an
environment
self is always bound to some object in any
environment
Determines
OOP Principles
Pi i l
A Example
An
E
l (S
(Scheme)
h
)
Suppose
pp
we evaluate ((odd 42)) in an inner
scope where even is defined to be
(define (even x) (= 0 (modulo x 2)))
Nothing changes odd calls original even
(static scope)
E
Example
l (R
(Ruby
b Subclasses)
S b l
)
class A
def even x
if x == 0 then true else
odd(x-1) end
end
d f odd
def
dd x
if x == 0 then false else
even(x-1)
( ) end
end
end
class B < A
def even x
x % 2 == 0
end
end
d
P
Perspectives
ti
on L
Late
t Binding
Bi di
P
Perspectives
ti
on L
Late
t Binding
Bi di
More reuse/abuse
L
Lower-Level
L
l Vi
View
L t Binding
Late
Bi di iin S
Scheme
h
Key idea: extend all methods to take an
extra argument
g
((i.e.,, self))
An object is a record (closure) holding
methods and fields
Self is passed as an explicit argument
everywhere
Message resolution always uses self
I This
Is
Thi R
Real?
l?
doesn
doesntt model Ruby, where methods can be
added/removed dynamically and an objects
class determines behavior
Space
B tt Engineering,
Better
E i
i
B
Better
tt Reality
R lit