Parametric polymorphism and Generics

Similar documents
Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

CSE 331 Software Design & Implementation

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Polymorphism (generics)

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

CSE 331 Software Design & Implementation

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

CSE 331 Software Design & Implementation

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017]

CSE 331. Generics (Parametric Polymorphism)

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

Exercise 8 Parametric polymorphism November 18, 2016

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

JAVA V Assertions Java, winter semester

The collections interfaces

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Java Generics. Lecture CS1122 Summer 2008

More than you ever wanted to know about. Java Generics. Jeff Meister CMSC 420 Summer 2007

JAVA V Source files Java, winter semester

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

Exercise 8 Parametric polymorphism November 17, 2017

Java Generics Java, summer semester

Software Engineering. Prof. Agostino Poggi

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

CMSC 202. Containers

PIC 20A Collections and Data Structures

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

Java Generics -- an introduction. Based on

Rules and syntax for inheritance. The boring stuff

Introducing Generics

CSCE 314 Programming Languages

Collections, Maps and Generics

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

#Students per correct answers

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Generic types. Announcements. Raw ArrayLists. Generic types (cont.) Creating a raw ArrayList: Accessing a raw ArrayList:

Closed book but one sheet, both sides, of A4 paper is allowed. Section 2.5 of the text Generics in the Java Programming Languages by Gilad Bracha

+ Abstract Data Types

CS-202 Introduction to Object Oriented Programming

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

CMSC 202H. Containers and Iterators

CSE 331 Summer 2017 Final Exam. The exam is closed book and closed electronics. One page of notes is allowed.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

generic programming alberto ferrari university of parma

CS108, Stanford Handout #8. Java Generics

COMP1008 An overview of Polymorphism, Types, Interfaces and Generics

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

Announcements. Java Graphics. Exceptions. Java Odds & Ends

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

PARAMETRIC POLYMORPHISM

Generics with Type Bounds

Practice for Chapter 11

The list abstract data type defined a number of operations that all list-like objects ought to implement:

Photo credit: Andrew Kennedy JAVA GENERICS

COMP6700/2140 Generic Methods

Subtyping for behaviour?

CMSC 341. Nilanjan Banerjee

Generics Collection Framework

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Announcements. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Intuition: Type Signature is a Specification.

Programming Languages and Techniques (CIS120)

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

Java Review: Objects

25. Generic Programming

Building Java Programs

Java 5 New Language Features

AP CS Unit 7: Interfaces. Programs

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Generic programming in OO Languages

Compiler Construction 2009/2010 Polymorphic Types and Generics

Software Development (cs2500)

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

Some examples and/or figures were borrowed (with permission) from slides prepared by Prof. H. Roumani. The Collection Framework

Polymorphism. return a.doublevalue() + b.doublevalue();

CompuScholar, Inc. 9th - 12th grades

Super-Classes and sub-classes

Generics method and class definitions which involve type parameters.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS260 Intro to Java & Android 03.Java Language Basics

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Collections Algorithms

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Transcription:

Parametric polymorphism and Generics

Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays CSCI 2600 Spring 2017 2

Polymorphism Subtype polymorphism What we discussed Code can use a subclass B where a superclass A is expected E.g., Code A a; a.m() is polymorphic, because a can be of many different types at runtime: it can be a A object or an B object. Code works with A and with B (with some caveats!) Standard in object-oriented languages CSCI 2600 Spring 2017 3

Polymorphism Parametric polymorphism Code takes a type as a parameter Implicit parametric polymorphism Explicit parametric polymorphism Standard in functional programming languages Overloading typically referred to as ad-hoc polymorphism CSCI 2600 Spring 2017 4

Implicit Parametric Polymorphism There is no explicit type parameter(s). Code is polymorphic because it works with many different types. E.g.: def intersect(sequence1, sequence2): result = [ ] for x in sequence1: if x in sequence2: result.append(x) return result As long as sequence1 and sequence2 are of some iterable type, intersect works! CSCI 2600 Spring 2017 5

Implicit Parametric Polymorphism In Python, Lisp, Scheme, others languages There is no explicit type parameter(s); the code works with many different types Usually, there is a single copy of the code, and all type checking is delayed until runtime If the arguments are of type as expected by the code, code works If not, code issues a type error at runtime CSCI 2600 Spring 2017 6

Explicit Parametric Polymorphism In Ada, C++, Java There is an explicit type parameter(s) Explicit parametric polymorphism is also known as genericity E.g. in C++ we have templates: template<class V> class list_node { list_node<v>* prev; template<class V> class list { list_node<v> header; CSCI 2600 Spring 2017 7

Explicit Parametric Polymorphism Instantiating classes from previous slide with int: typedef list_node<int> int_list_node; typedef list<int> int_list; Usually templates are implemented by creating multiple copies of the generic code, one for each concrete type argument, then compiling Problem: if you instantiate with the wrong type argument, C++ compiler gives us long, cryptic error messages referring to the generic (templated) code in the STL :) CSCI 2600 Spring 2017 8

Explicit Parametric Polymorphism Java generics work differently from C++ templates: more type checking on generic code OO languages usually have both: subtype polymorphism through inheritance: A extends B or A implements B) and explicit parametric polymorphism referred to as generics or templates CSCI 2600 Spring 2017 9

Using Java Generics List<AType> list = new ArrayList<AType>(); AType is the type argument. We instantiated generic (templated) class ArrayList with concrete type argument AType List<String> names = new ArrayList<String>(); names.add( Ana ); names.add( Katarina ); String s = names.get(0); // what happens here? Point p = names.get(0); // what happens here? Point p = (Point) names.get(0); // what happens? CSCI 2600 Spring 2017 10

Defining a Generic Class Declaration of type parameter class MySet<T> { // rep invariant: non-null, // contains no duplicates List<T> therep; T lastlookedup; Use of type parameter CSCI 2600 Spring 2017 11

Defining a Generic Class // generic (templated, parameterized) class public class Name<TypeVar, TypeVar> { Convention: TypeVar is 1-letter name such as T for Type, E for Element, N for Number, K for Key, V for Value Class code refers to the type parameter E.g., E To instantiate a generic class, client supplies type arguments E.g., String as in List<String> name; Think of it as invoking a constructor for the generic class CSCI 2600 Spring 2017 12

Example: a Generic Interface // Represents a list of values public interface List<E> { public void add(e value); public void add(int index, E value); public E get(int index); public int indexof(e value); public boolean isempty(); public void remove(int index); public void set(int index, E value); public int size(); public class ArrayList<E> implements List<E> { public class LinkedList<E> implements List<E> { CSCI 2600 Spring 2017 13

Generics Clarify Your Code Without generics This is known as pseudo-generic containers interface Map { Object put(object key, Object value); Object get(object key); Client code: Map nodes2neighbors = new HashMap(); String key = HashSet value = nodes2neigbors.put(key,value); HashSet neighbors = (HashSet) nodes2neighbors.get(key); Casts in client code. Clumsy. If client mistakenly puts non-hashset value in map, ClassCastException at this point. CSCI 2600 Spring 2017 14

Generics Clarify Your Code With generics interface Map<K,V> { V put(k key, V value); V get(k key); No casts. Compile-time checks prevent client from putting non-hashset value. Somewhat clumsy, verbose instantiations. Client code: Map<String,HashSet<String>> nodes2neighbors = new HashMap<String,HashSet<String>>(); String key = nodes2neigbors.put(key,value); HashSet<String> neighbors = nodes2neighbors.get(key); CSCI 2600 Spring 2017 15

Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays CSCI 2600 Spring 2017 16

Bounded Types Restrict Instantiation by Client interface MyList1<E extends Object> { MyList1 can be instantiated with any type. Same as interface MyList1<E> { Upper bound on type argument interface MyList2<E extends Number> { MyList2 can be instantiated only with type arguments that are Number or subtype of Number MyList1<Date> // OK MyList2<Date> // what happens here? CSCI 2600 Spring 2017 17

Why Bounded Types? Generic code can perform operations permitted by the bound class MyList1<E extends Object> void m(e arg) { arg.intvalue();//compile-time error; Object //does not have intvalue() class MyList2<E extends Number> void m(e arg) { arg.intvalue();//ok. Number has intvalue() CSCI 2600 Spring 2017 18

Another Example public class Graph<N> implements Iterable<N> private final Map<N, Set<N>> node2neighbors; public Graph(Set<N> nodes, Set<Tuple<N,N>> edges) { public interface Path<N, P extends Path<N,P>> extends Iterable<N>, Comparable<Path<?,?>> { public Iterator<N> iterator(); P extend(n n); CSCI 2600 Spring 2017 19

Bounded Type Parameters <Type extends SuperType> An upper bound, type argument can be SuperType or any of its subtypes <Type super SubType> A lower bound, type argument can be SubType or any of its supertypes CSCI 2600 Spring 2017 20

Exercise Given this hierarchy with X, Y and Z: What are valid instantiations of generics class A<T extends X> {? class A<T extends Z> {? Y X Z class A<T super Z> {? class A<T super X> {? CSCI 2600 Spring 2017 21

Declaring a Generic Method class MyUtils { <T extends Number> T sumlist(collection<t> l) { Declaration of type parameter Uses of type parameter CSCI 2600 Spring 2017 22

Generic Method Example: Sorting public static <T extends Comparable<T>> void sort(list<t> list) { // use of get & T.compareTo<T> // T e1 = list.get( ); // T e2 = list.get( ); // e1.compareto(e2); We can use T.compareTo<T> because T is bounded by Comparable<T>! CSCI 2600 Spring 2017 23

Another Generic Method Example public class Collections { public static <T> void copy(list<t> dst, List<T> src) { for (T t : src) { dst.add(t); When you want to make a single (often static) method generic in a class, precede its return type by type parameter(s). CSCI 2600 Spring 2017 24

More Bounded Type Examples <T extends Comparable<T>> T max(collection<t> c); <T> void copy(list<t2 super T> dst, List<T3 extends T> src); (actually, must use wildcard? --- more on this later: <T> void copy(list<? super T> dst, List<? extends T> src); ) <T extends Comparable<T2 super T>> void sort(list<t> list) (same, must use? with super: <? super T>) CSCI 2600 Spring 2017 25

Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards Type erasure Java arrays CSCI 2600 Spring 2017 26

Generics and Subtyping Integer is a subtype of Number Number Is List<Integer> a subtype of List<Number>? List<Number>? List<Integer> Integer CSCI 2600 Spring 2017 27

Use Function Subtyping Rules to Find Out! List<Number> interface List<Number> { boolean add(number elt); Number get(int index); interface List<Integer> { boolean add(integer elt); Integer get(int index); Function subtyping: subtype must have supertype parameters and subtype return!? List<Integer> CSCI 2600 Spring 2017 28

What is the Subtyping Relationship Between List<Number> and List<Integer> Thus, List<Number> and List<Integer> are unrelated through subtyping! List<Number> List<Integer> CSCI 2600 Spring 2017 29

Immutable Lists interface ImmutableList<Number> { Number get(int index); interface ImmutableList<Integer> { Integer get(int index); ImmutableList<Number> ImmutableList<Integer> CSCI 2600 Spring 2017 30?

Write-only Lists interface WriteOnlyList<Number> { boolean add(number elt); interface WriteOnlyList<Integer> { boolean add(integer elt); Is WriteOnlyList<Integer> subtype of WriteOnlyList<Number> NO! Is WriteOnlyList<Number> subtype of WriteOnlyList<Integer> YES! CSCI 2600 Spring 2017 31

Getting Stuff Out of WriteList interface WriteList<Number> { boolean add(number elt); Number get(int index); interface WriteList<Integer> { boolean add(integer elt); Object get(int index); Is WriteList<Number> subtype of WriteList<Integer> YES! Contravariant subtyping: because the subtyping relationship between the composites (WriteList<Number> is subtype of WriteList<Integer>) is the opposite of the subtyping relationship between their type arguments (Integer is subtype of Number) CSCI 2600 Spring 2017 32

Invariance is Restrictive (Because it Disallows Subtyping) Java solution: wildcards interface Set<E> { // Ads all elements in c to this set // if they are not already present. void addall(set<e> c); void addall(collection<e> c); void addall(collection<? extends E> c); Not good. Can t have Set<Number> s; List<Number> l; s.addall(l); // List & Set unrelated <T extends E> void addall(collection<t> c); Not good either. Can t have Set<Number> s; List<Integer> l; s.addall(l); This is because of invariance: List<Integer> is a subtype of Collection<Integer> but Collection<Integer> is not a subtype of Collection<Number>! CSCI 2600 Spring 2017 Solution: wildcards.? Is the wildcard. 33

Java Wildcards A wildcard is essentially an anonymous type variable Use? if you d use a type variable exactly once? appears at the instantiation site of the generic (also called use site) As opposed to declaration site (also called definition site: where type parameter is declared) Purpose of the wildcard is to make a library more flexible and easier to use by allowing limited subtyping CSCI 2600 Spring 2017 34

Using Wildcards This is instantiation of the generic Collection class HashSet<E> implements Set<E> { void addall(collection<? extends E> c) { // What does this give us about c? // i.e., what can code assume about c? // What operations can code invoke on c? Wildcard appears at instantiations (uses) of generics There is also <? super E> Intuitively, why <? extends E> makes sense here? CSCI 2600 Spring 2017 35

Covariance? Introduces covariant subtyping Apple is a subtype of Fruit List<Apple> is subtype of List<? extends Fruit> List<Apple> apples = new ArrayList<Apple>(); List<? Extends Fruit> fruits = apples; apples.add(new Strawberry()); Won t compile fruits.add(new Fruit()); won t compile either CSCI 2600 Spring 2017 36

Covariance the? extends T wildcard tells the compiler that we're dealing with a subtype of the type T, b but we cannot know which one. Since there's no way to tell, and we need to guarantee type safety you won't be allowed to put anything inside such a structure. since we know that whichever type it might be, it will be a subtype of T, we can get data out of the structure with the guarantee that it will be a T instance Fruit get = fruits.get(0); CSCI 2600 Spring 2017 37

Legal Operations on Wildcards Object o; Number n; Integer i; PositiveInteger p; List<? extends Integer> lei; First, which of these is legal? lei = new ArrayList<Object>(); lei = new ArrayList<Number>(); lei = new ArrayList<Integer>(); lei = new ArrayList<PositiveInteger>(); lei = new ArrayList<NegativeInteger>(); Which of these is legal? lei.add(o); lei.add(n); lei.add(i); lei.add(p); lei.add(null); o = lei.get(0); n = lei.get(0); i = lei.get(0); p = lei.get(0); CSCI 2600 Spring 2017 38

Contravariance List<Fruit> fruits = new ArrayList<Fruit>(); List<? super Apple> = fruits; fruits is a reference to a List of something that is a supertype of Apple. Apple and any of its subtypes will be assignment compatible with fruits These will compile fruits.add(new Apple()); fruits.add(new GreenApple()); These will not fruits.add(new Fruit()); fruits.add(new Object()); CSCI 2600 Spring 2017 39

Contravariance Since we cannot know which supertype it is, we aren't allowed to add instances of any. The only thing you can get out of it will be Object instances since we cannot know which supertype it is the compiler can only guarantee that it will be a reference to an Object, since Object is the supertype of any Java type. CSCI 2600 Spring 2017 40

Legal Operations on Wildcards Object o; Number n; Integer i; PositiveInteger p; Which of these is legal? lsi.add(o); lsi.add(n); lsi.add(i); lsi.add(p); List<? super Integer> lsi; lsi.add(null); First, which of these is legal? o = lsi.get(0); lsi = new ArrayList<Object>(); n = lsi.get(0); lsi = new ArrayList<Number>(); i = lsi.get(0); lsi = new ArrayList<Integer>(); lsi = new ArrayList<PositiveInteger>(); p = lsi.get(0); CSCI 2600 Spring 2017 41

How to Use Wildcards Use <? extends T> when you get (read) values from a producer Use <? super T> when you add (write) values into a consumer E.g.: <T> void copy(list<? super T> dst, List<? extends T> src) PECS: Producer Extends, Consumer Super Use neither, just <T>, if both add and get CSCI 2600 Spring 2017 42

PECS Use the? extends wildcard if you need to retrieve object from a data structure. Use the? super wildcard if you need to put objects in a data structure. If you need to do both things, don't use any wildcard. CSCI 2600 Spring 2017 43

CSCI 2600 Spring 2017 44

Wildcards allow Subtyping for Generics Object Number Integer List<Object> List<Number> List<?> List<? extends Number> List<Integer> List<Double> ArrayList<Integer> List<Integer> CSCI 2600 Spring 2017 45

Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards (briefly) Type erasure Arrays CSCI 2600 Spring 2017 46

Type Erasure All type arguments become Object when compiled Reason: backward compatibility with old bytecode At runtime all generic instantiations have same type List<String> lst1 = new ArrayList<String>(); List<Integer> lst2 = new ArrayList<Integer>(); lst1.getclass() == lst2.getclass() // true Cannot use instanceof to find type argument Collection<?> cs = new ArrayList<String>(); if (cs instanceof Collection<String>) { // compile-time error Must use equals() on elements of generic type CSCI 2600 Spring 2017 47

Equals for a Generic Class class Node<E> { @Override At runtime, JVM has no knowledge of type argument. Node<String> is same as Node<Elephant>. Instanceof is a compiletime error. public boolean equals(object obj) { if (!(obj instanceof Node<E>)) return false; Node<E> n = (Node<E>) obj; return this.data().equals(n.data()); CSCI 2600 Spring 2017 48

Equals for a Generic Class class Node<E> { @Override public boolean equals(object obj) { if (!(obj instanceof Node<E>)) { return false; Node<E> n = (Node<E>) obj; return this.data().equals(n.data()); Same here. JVM has no knowledge of type argument. Node<String> will cast to Node<Elephant>. Casting results in a compile-time warning, but not error. CSCI 2600 Spring 2017 49

Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards (briefly) Type erasure Arrays CSCI 2600 Spring 2017 50

Number Arrays and Subtyping Integer Integer is subtype of Number Is Integer[] a subtype of Number[] Use our subtyping rules to find out (Just like with List<Integer> and List<Number>) Again, the answer is NO! Different answer in Java: in Java Integer[] is a Java subtype Number[]! The Java subtype is not a true subtype! Known as problem with Java s covariant arrays Number[]? Integer[] CSCI 2600 Spring 2017 51

Integer[] is a Java subtype of Number[] Number n; ia = na; //what Number[] na; // happens here? Integer i; Double d = 3.14; Integer[] ia; na = ia; //OK! na[0] = n; na[2] = d; na[1] = i; i = ia[2]; //what n = na[0]; // happens here? i = na[1]; //what happens? ia[0] = n; //what happens? ia[1] = i; n = ia[0]; i = ia[1]; CSCI 2600 Spring 2017 52

Writing a Generic Class Start by writing a concrete class Make sure it is correct Reasoning Testing Think about writing a second concrete class Generalize by adding type parameters (generics) Which are the same, which differ Compiler will find errors With practice, it gets easier to start with generics CSCI 2600 Spring 2017 53