DOC PREVIEW
UW CSE 341 - Lecture Notes

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%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

UW CSE 341 - Lecture Notes

Documents in this Course
Macros

Macros

6 pages

Macros

Macros

6 pages

Macros

Macros

3 pages

Mutation

Mutation

10 pages

Macros

Macros

17 pages

Racket

Racket

25 pages

Scheme

Scheme

9 pages

Macros

Macros

6 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?