6 objects
TRANSCRIPT
-
8/10/2019 6 objects
1/17
CSE 413 Autumn 2008
Objects & DynamicDispatch
-
8/10/2019 6 objects
2/17
an
Weve learned a great deal about functionaland object-oriented programming
Now,
Look at semantics and rinci les more carefull
Look at O-O and functional programming whatare the essential differences and similarities
-
8/10/2019 6 objects
3/17
oo - p u es
Key idea in any language: how ares mbols names identifiers resolved
Functional programming first-class, , . .,
dont use set!)
-
8/10/2019 6 objects
4/17
oo - p u es n u y
In Ruby, use syntactic distinctions instance fields x , class fields x vs
method/block variables and method names (x)
No shadowing of fields, unlike Java Can shadow method names with variables
So: is m+2 a variable looku or a method call?
We wont worry about this for the most part
-
8/10/2019 6 objects
5/17
rs - ass
If something can be computed, stored in fields/variables, used as arguments, returned as results,we sa it is first-class
All objects in Ruby are first-class
& most things are objects ngs a are no :
Message names cant write x.(if b then m else n end)
Blocks (but procs are)Argument lists
-
8/10/2019 6 objects
6/17
ar a e oo up n u y
To resolve a variable (e.g., x) Inside a code block x e , x resolves to local
variable (the argument)
Not strictly true in Ruby 1.8 & earlier if x already exists
Else x resolves to x defined in enclosing method
Lexical sco e as in Scheme
Implies Ruby implementation needs to build closures atleast some of the time
-
8/10/2019 6 objects
7/17
essage oo up n u y
To resolve a message (e.g., m)All messages are sent to an object (e.g., e.m), so
Get class of obj (e.g., A) Every object has a class and carries a reference to the
If m defined in A (instance methods first, thenclass methods), call it, otherwise recursively look
Mixins complicate this somewhat (later) If no match up the chain, method not found error
-
8/10/2019 6 objects
8/17
a s se
Evaluation always takes place in anenvironment
self is always bound to some object in any
Determines resolution for self and super
-
8/10/2019 6 objects
9/17
r ncpes
Inheritance and override Private fields (just abstraction) The semantics of message send
To send m to obj means evaluate body of methodmap to arguments and self is bound to obj
This is exactly late binding, dynamic dispatch,
And why superclass code can call code defined insubclasses
-
8/10/2019 6 objects
10/17
n xamp e c eme
Suppose this is defined at top-level(define (even x) (if (= x 0) #t (odd (- x 1))))
(define (odd x) (if (= x 0) #f (even (- x 1))))
Suppose we evaluate odd 42 in an innerscope where even is defined to be
(define (even x) (= 0 (modulo x 2)))
Nothing changes odd calls original even(static scope)
-
8/10/2019 6 objects
11/17
xamp e u y u c asses
class Adef even x
==
class B < Adef even x
odd(x-1) end
end
x % 2 == 0
ende o x
if x == 0 then false else
even(x-1) end
en
Now odd, as well as
endend
,instances of B
-
8/10/2019 6 objects
12/17
erspec ves on a e n ng
More complicated semanticsRub without self is easier to define and
reason about
Seems natural onl because ou have hadmonths of this in previous courses
Hard to reason about code which method is
really called here?
-
8/10/2019 6 objects
13/17
erspec ves on a e n ng
But often an elegant pattern for reuseOO without self is not OO
Fits well with object analogy
code even when other code wasnt written tobe specialized
More reuse/abuse
-
8/10/2019 6 objects
14/17
ower- eve ew
A definition in one language is often a patternin another an s mu a e a e- n ng n c eme eas y
enough
objects and late binding are implementedNave, but accurate view can give a way to
,implementations contain more sophisticatedengineering
-
8/10/2019 6 objects
15/17
a e n ng n c eme
Key idea: extend all methods to take anextra ar ument i.e. self
An object is a record (closure) holding
Self is passed as an explicit argument
Message resolution always uses self
-
8/10/2019 6 objects
16/17
s s ea
Its a fine pattern, but It doesnt model Rub where methods can be
added/removed dynamically and an objects
class determines behavior In the example we model classless objects
Space inefficient duplicate methods
Time inefficient method lookup needs to beconstant time in real systems
-
8/10/2019 6 objects
17/17
etter ng neer ng, etter ea ty
To model classes, add a level ofindirection: all instances created b thesame constructor share a list of methods
And for Rub we can chan e the list
Use better data structures (array or hash)
-And add tricks so subclassing works