CSE341: Programming Languages Lecture 27 Generics vs. Subtyping; Bounded Polymorphism Dan Grossman Fall 2011.

Slides:



Advertisements
Similar presentations
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Advertisements

CSE341: Programming Languages Lecture 23 OO vs. Functional Decomposition; Adding Operations & Variants; Double-Dispatch Dan Grossman Fall 2011.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE341: Programming Languages Lecture 12 Equivalence Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 13 Equivalence; Parametric Polymorphism Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 17 Structs, Implementing Languages, Implementing Higher-Order Functions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 25 Subtyping for Records and Functions Dan Grossman Fall 2011.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Programming LanguagesSection 81 Programming Languages Section 8. OOP v.s. FP, Subtyping Xiaojuan Cai Spring 2015.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Types in programming languages1 What are types, and why do we need them?
CMSC 330: Organization of Programming Languages Java Generics.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
CSE 341 Lecture 11 b closures; scoping rules
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

CSE341: Programming Languages Lecture 27 Generics vs. Subtyping; Bounded Polymorphism Dan Grossman Fall 2011

Today 1.Compare generics and subtyping –What each is good for 2.Combine generics and subtyping to get even more benefit –Example in Java, but as always, ideas more general Fall 20112CSE341: Programming Languages

What are generics good for? Some good uses for parametric polymorphism: Types for functions that combine other functions: Types for functions that operate over generic collections Many other idioms General point: When types can "be anything" but multiple things need to be "the same type" Fall 20113CSE341: Programming Languages fun compose (g,h) = fn x => g (h x) (* compose : ('b -> 'c) * ('a -> 'b) -> ('a -> 'c) *) val length : 'a list -> int val map : ('a -> 'b) -> 'a list -> 'b list val swap : ('a * 'b) -> ('b * 'a)

Generics in Java Java generics a bit clumsier syntactically and semantically, but can express the same ideas –Without closures, often need to use (one-method) objects –See also lecture on closures in Java/C Simple example without higher-order functions: Fall 20114CSE341: Programming Languages class Pair { T1 x; T2 y; Pair(T1 _x, T2 _y){ x = _x; y = _y; } Pair swap() { return new Pair (y,x); } … }

Subtyping is not good for this Using subtyping for containers is much more painful for clients –Have to downcast items retrieved from containers –Downcasting has run-time cost –Downcasting can fail: no static check that container has the type of data you think it does –(Only gets more painful with higher-order functions like map ) Fall 20115CSE341: Programming Languages class LamePair { Object x; Object y; LamePair(Object _x, Object _y){ x=_x; y=_y; } LamePair swap() { return new LamePair(y,x); } } // error caught only at run-time: String s = (String)(new LamePair("hi",4).y);

What is subtyping good for? Some good uses for subtype polymorphism: Code that "needs a Foo" but fine to have "more than a Foo" –Geometry on points works fine for colored points –GUI widgets specialize the basic idea of "being on the screen" and "responding to user actions" Related perspective: Writing code in terms of what it expects of arguments (but more is fine) –Static checking makes sure arguments have what is needed Fall 20116CSE341: Programming Languages

Awkward in ML ML does not have subtyping, so this simply does not type-check: Fall 20117CSE341: Programming Languages fun distToOrigin ({x=x,y=y} : {x:real,y:real}) = Math.sqrt(x*x + y*y) val five = distToOrigin {x=3.0,y=4.0,color="red"}

Higher-order workaround Can write reusable code in ML a la subtyping if you plan ahead and use generics in awkward ways See example in lec27.sml Fall 20118CSE341: Programming Languages

Wanting both Could a language have generics and subtyping? –Sure! More interestingly, want to combine them –"Any type T1 that is a subtype of T2 " –This is bounded polymorphism –Lets you do things naturally you can't do with generics or subtyping Fall 20119CSE341: Programming Languages

Example [also see Lec27.java] Only bounded polymorphism lets us use inCircle with a list of ColorPt objects –And callee can't put a Pt in pts or the result list! Fall CSE341: Programming Languages class Pt { … double distance(Pt p) { … } } class ColorPt extends Pt { … } class Pt { static List inCircle(List pts, Pt center, double r) { List result = new ArrayList (); for(T pt: pts) if(pt.distance(center) <= r) result.add(pt); return result; }

One caveat For backward-compatibility and implementation reasons, in Java there is always a way to use casts to get around the static checking with generics –With or without bounded polymorphism Oh well Fall CSE341: Programming Languages