'&$%CSE 341:Programming LanguagesHal PerkinsSpring 2011Lecture 25— Extensibility in OO and FPHal Perkins CSE341 Spring 2011, Lecture 25 1'&$%One-of types and operations• Given a type with several variants/subtypes and severalfunctions/methods, there’s a 2D-grid of code you need:Int Negate Add MultevaltoStringhasZero• OO and FP just lay out the code differently!!!• Which is more convenient depends on what you’re doing andhow the variants/operations “fit together”• Often, tools let you view “the other dimension”• Opinion: Dimensional structure of code is greater than 2–3, sowe’ll never have exactly what we want in text.Hal Perkins CSE341 Spring 2011, Lecture 25 2'&$%ExtensibilityLife gets interesting if need to extend code w/o changing existingcode.• ML makes it easy to write new operations; Java does not• Java makes it easy to write new variants; ML does not• In ML the original code must plan for extensibility usingpolymorphism and function arguments• In Java the original code must plan for extensibility using“extra” abstract methods– (see “the visitor pattern” on your own)Hal Perkins CSE341 Spring 2011, Lecture 25 3'&$%UnextensibilityExtensibility is not all it’s cracked up to be:• Makes original code more difficult to change later• Makes code harder to reason about locally (e.g., dynamicdispatch or functions-as-arguments mean you never know whatcode might execute next)ML and Java have different defaults, but both let you decide whatto make extensible:• ML: Generally less extensible. Without a type constructor or afunction-argument, you limit what might happen (thanks toclosed recursion)• Java: Generally extensible by default. But you can declaremethods or classes final; arguably under-used.Hal Perkins CSE341 Spring 2011, Lecture 25
View Full Document