'&$%CSE 341:Programming LanguagesDan GrossmanSpring 2008Lecture 21— Duck Typing, Blocks & Proces & Iterators, Inheritance& OverridingDan Grossman CSE341 Spring 2008, Lecture 21 1'&$%TodayThree separate topics (mostly the last one?)1. “Duck Typing” (almost got there on Friday)2. Blocks and iterators (closures in Ruby)3. Subclassing (inheritance, overriding, dynamic-dispatch, somedesign issues)Textbook and/or Section: Essential stuff for homework 6• Much more on Array and Hash• Exploratory programming• Cloning• More on blocks and iteratorsDan Grossman CSE341 Spring 2008, Lecture 21 2'&$%Duck Typing“If it walks like a duck and quacks like a duck, it’s a duck.”A method might think, “I need a Foo” but really it only needs anobject that has similar enough methods that it acts enough like a Foothat the method works.Embracing duck typing: Methods that make method calls rather thanassume the class of their argument.Plus: More code reuse, very OO approach• What messages can some object receive is all that mattersMinus: Almost nothing is equivalent• x+x versus x*2 versus 2*x• Callees may not want callers assuming so muchDan Grossman CSE341 Spring 2008, Lecture 21 3'&$%Blocks and IteratorsMany methods in Ruby “take a block,” which is a “special” thingseparate from the argument list.They are used very much like closures in functional programming; cantake 0 or more arguments (see examples)The preferred way for iterating over arrays, doing something n times,etc.They really are closures (can access local variables where they weredefined).Useful on homework: each, possibly injectUseful in Ruby: many, many moreDan Grossman CSE341 Spring 2008, Lecture 21 4'&$%Blocks vs. ProcsThese block arguments can be used only by the “immediate” callee viathe yield keyword.If you really want a “first-class object” you can pass around, store infields, etc., convert the block to an instance of Proc.• lambda {|x,y,z| e}• Instances of Proc have a method call• This really is exactly a closure.Actually, there is a way for the caller to pass a block and the calleeconvert it to a Proc.• Look it up if you’re curious.• This is what lambda does(just a method in Object that returns the Proc it creates)Dan Grossman CSE341 Spring 2008, Lecture 21 5'&$%SubclassesRuby is dynamically typed, so subclassing is not about whattype-checks.Subclassing is about inheriting methods from the superclass.• In Java, it’s about inheriting fields too, but we can just write toany field we want.Example: ThreeDPoint inherits methods x and y.Example: ColorPoint inherits distFromOrigin anddistFromOrigin2.Dan Grossman CSE341 Spring 2008, Lecture 21 6'&$%OverridingIf it were just inheritance, then with dynamic typing subclassing wouldjust be avoiding copy/paste.It’s more.But first, “simple” overriding lets us redefine methods in the subclass.• Often convenient to use super to use superclass definition in ourdefinition.This is still “just” avoiding copy-paste.Example: distFromOrigin and initialize in ThreeDPoint.Dan Grossman CSE341 Spring 2008, Lecture 21 7'&$%Ruby-ish DigressionWhy make a subclass when we could just add/change methods to theclass itself?• Add a color field to Point itself• Affects all Point instances, even those already created (!)Plus: Now a ThreeDPoint has a color field too.Minus: Maybe that messes up another part of your program.Fun example: Redefining Fixnum’s + to return 5.Dan Grossman CSE341 Spring 2008, Lecture 21 8'&$%Late-BindingSo far, this OO stuff is honestly very much like functional programming• Fields are just like things in a closure’s environmentBut this is totally different:• When a method defined in a superclass makes a self call itresolves to the method defined in the subclass (typically viaoverriding)Example: distFromOrigin2 in PolarPoint still works correctly!!!Next lecture: Studying this very carefully.Dan Grossman CSE341 Spring 2008, Lecture 21
View Full Document