DOC PREVIEW
UW CSE 341 - Lecture Notes

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 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 8 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 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 341:Programming LanguagesDan GrossmanWinter 2008Lecture 20— Inheritance & Overriding, Blocks & IteratrorsDan Grossman CSE341 Winter 2008, Lecture 20 1'&$%TodayTwo separate topics (mostly the first one)1. Subclassing (inheritance, overriding, dynamic-dispatch, somedesign issues)2. Blocks and iterators (closures in Ruby)Section: Essential stuff for homework 6• Arrays and iterators on them• Hashes• More on blocks and iteratorsDan Grossman CSE341 Winter 2008, Lecture 20 2'&$%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 Winter 2008, Lecture 20 3'&$%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 Winter 2008, Lecture 20 4'&$%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 Winter 2008, Lecture 20 5'&$%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!!!Friday’s lecture: Studying this very carefully.Dan Grossman CSE341 Winter 2008, Lecture 20 6'&$%Blocks and IteratorsMany methods in Ruby “take a block,” which is a “special” thingseparate from the argume nt list.They are used very much like closures in functional programming; cantake 0 or more arguments (s ee 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: any?, eachUseful in Ruby: many, many moreDan Grossman CSE341 Winter 2008, Lecture 20 7'&$%Blocks vs. ProcsThese block arguments can be used only by the “immediate” callee viathe yield keyword.If you really want a “first-c lass 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 returnsthe Proc it creates)Dan Grossman CSE341 Winter 2008, Lecture 20


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?