CSE 331 Software Design and Implementation. Lecture 14 Generics 1

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

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

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

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Polymorphism (generics)

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

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

Parametric polymorphism and Generics

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017]

CSE 331. Generics (Parametric Polymorphism)

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

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

COMP6700/2140 Generic Methods

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

Software Engineering. Prof. Agostino Poggi

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

Exercise 8 Parametric polymorphism November 17, 2017

CSE 331 Software Design and Implementation. Lecture 4 Specifications

Specifications. CSE 331 Spring 2010

Announcements. Specifications. Outline. Specifications. HW1 is due Thursday at 1:59:59 pm

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

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

Advanced Programming Generics Collections

Javarifier: inference of reference immutability

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

Exercise 8 Parametric polymorphism November 18, 2016

Lecture 4 Specifications

Generic Universe Types in JML

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Quiz. Programming Languages. CSE 130 : Fall Lecture 16: Static Types for Objects. Ranjit Jhala UC San Diego

CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping. Dan Grossman Winter 2013

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Java Generics -- an introduction. Based on

Comp215: Thinking Generically

Refactoring Java Generics by Inferring Wildcards, In Practice

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

Chapter 4 Defining Classes I

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

CSCE 314 Programming Languages

generic programming alberto ferrari university of parma

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

Self Type Constructors. Atsushi Igarashi Kyoto University Joint work with Chieri Saito

Last name:... First name:... Department (if not D-INFK):...

Photo credit: Andrew Kennedy JAVA GENERICS

Generics with Type Bounds

Representation Invariants and Abstraction Functions

CSE 331 Software Design & Implementation

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

Recap. What is a type? Types in OO languages. What is a type? Types as sets. Up next, some advanced OO topics

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Why Use Generics? Generic types Generic methods Bounded type parameters Generics, Inheritance, and Subtypes Wildcard types

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

Comparing procedure specifications

Lecture 7 Abstraction Functions

Primitive Java Generic Class

Object Oriented Issues in VDM++

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

The collections interfaces

301AA - Advanced Programming [AP-2017]

Lecture 5 Representation Invariants

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

CSE 331 Final Exam 3/16/15 Sample Solution

What is a type? Types in OO languages. What is a type? Types as sets. Example. Clients of screensaver

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

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

CS 251 Intermediate Programming Inheritance

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

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

JAVA V Assertions Java, winter semester

Lecture 7: Data Abstractions

Inheritance and Interfaces

Lecture 5 Abstract Data Types

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

A Short Summary of Javali

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

Generic programming in OO Languages

Subtypes and Subclasses

Fortgeschrittene objektorientierte Programmierung (Advanced Object-Oriented Programming)

Collections, Maps and Generics

MIDTERM EXAMINATION - CS130 - Spring 2003

Array. Prepared By - Rifat Shahriyar

Functions as Parameters & Functional Interfaces. Lecture 12 COMP110 Spring 2018

So you think you got it?

CS558 Programming Languages

JAVA V Source files Java, winter semester

Generics, I/O & Regular Expressions Maria Zontak

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Generics. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 10

Outline. Announcements. Feedback. CS1007: Object Oriented Design and Programming in Java. Java beans Applets

CSE 331 Software Design & Implementation

Symmetry in Type Theory

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

Testing and Debugging

Transcription:

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

Announcements

Announcements Quiz 5 is due Thursday Homework 6 due Thursday Midterm grades and feedback will be out this evening

Generics

Outline (lec14 and lec15) Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Digression: Java s unsoundness(es) Java realities: type erasure

Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1 + y1*y1); Math.sqrt(x2*x2 + y2*y2); Abstraction over data: Data structures Point p1, p2; Abstraction over implementations: Specifications * @requires x >= 0 * @return square root of x Abstraction over types: polymorphism (generics) Point<Integer>, Point<Double> Today!

Why we abstraction Hide details Avoid distraction Permit details to change later Give a meaningful name to a concept Permit reuse in new contexts Avoid duplication: error-prone, confusing Save reimplementation effort Helps to Don t Repeat Yourself

Related abstractions interface ListOfStrings { boolean add(string elt); String get(int index); interface ListOfNumbers { boolean add(number elt); Number get(int index);

Related abstractions interface ListOfStrings { boolean add(string elt); String get(int index); interface ListOfNumbers { boolean add(number elt); Number get(int index); and many, many more // Type abstraction // abstracts over element type E interface List<E> { boolean add(e n); E get(int index); Type abstraction lets us use these types: List<String> List<Number> List<Integer> List<List<String>>

Formal parameter vs. type parameter interface ListOfIntegers { boolean add(integer elt); Integer get(int index); Declares a new variable elt, called a (formal) parameter Instantiate by passing in an argument interpretable as Integer E.g., lst.add(7) Scope of elt (declared in method header) is the entire method body interface List<E> { boolean add(e n); E get(int index); Declares a new type variable E, called a type parameter Instantiate by passing in an argument interpretable as any reference type E.g., List<String> Scope of E (declared in class header) is the entire class

Scope Declaration Use class NewSet<E> implements Set<E> { // rep invariant: // non-null, contains no duplicates // Use List<E> therep; E lastiteminserted; Use

Declaring and instantiating generics Parameter Parameter class MyClass<TypeVar1,, TypeVarN> { interface MyInterface<TypeVar1,, TypeVarN> { Convention: Type variable has one-letter name such as: T for Type, E for Element, K for Key, V for Value, To instantiate a generic class/interface, client supplies type arguments: MyClass<String,, Date> = new MyClass<>(); Argument Argument

Restricting instantiations by clients boolean add1(object elt); boolean add2(number elt); add1(new Date()); // OK add2(new Date()); // compile-time error type parameter s interface List1<E extends Object> { upper bound restricts which interface List2<E extends Number> { type arguments can be passed in List1<Date> // OK, Date is a subtype of Object List2<Date> // compile-time error, Date is not a // subtype of Number method parameter s type restricts which arguments can be passed in

Declaring and instantiating generics: syntax with bounds class MyClass<TypeVar1 extends TypeBound1,..., TypeVarN extends TypeBoundN> { (same for interface definitions) (default upper bound is Object) To instantiate a generic class/interface, client supplies type arguments: MyClass<String,, Date> Compile-time error if type is not a subtype of the upper bound

Using type variables Code can perform any operation permitted by the bound Because we know all instantiations will be subtypes! class Foo1<E extends Object> { void m(e arg) { arg.asint(); // compiler error, E might not... // support asint() class Foo2<N extends Number> { void m(n arg) { arg.asint(); // OK, since Number and its... // subtypes support asint()

More bounds <TypeVar extends SuperType> One upper bound; accepts given supertype or any of its subtypes <TypeVar extends ClassA & InterfaceB & InterfaceC & > Multiple upper bounds (superclass/interfaces) with & accepts an argument that matches all the bounds public class TreeSet<T extends Comparable<T>> {... Recursively-defined bounds TreeSet accepts any type that can be compared to itself

Outline Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Digression: Java s unsoundness(es) Java realities: type erasure

Generic Methods

Generic classes are not enough class Utils { public static double sumlist(list<number> lst) { double result = 0.0; for (Number n : lst) { result += n.doublevalue(); return result; public static Object choose(list<object> lst) { int i = // random number < lst.size return lst.get(i); Reminder: static means no receiver (this parameter). Cannot pass List<Kitten> Cannot pass List<Double> List<Double> is not a subtype of List<Number>! We will see why soon.

Weaknesses of generic classes Would like to use sumlist for any subtype of Number For example, Double or Integer But as we will see, List<Double> is not a subtype of List<Number> Would like to use choose for any element type i.e., any subclass of Object Want to tell clients more about return type than Object Class Utils is not generic, but the methods should be generic

Generic methods solve the problem class Utils { public static double sumlist(list<t1> lst){ double result = 0.0; for (Number n : lst) { // T1 also works result += n.doublevalue(); return result; public static T2 choose(list<t2> lst) { int i = // random number < lst.size return lst.get(i); error: cannot find symbol: class T1 error: cannot find symbol: class T2 Need to ensure T1 subtype of Number

Generic methods solve the problem class Utils { public static <T1 double extends sumlist(list<t1> Number> lst){ double result = 0.0; for (Number n : lst) { // T1 also works result += n.doublevalue(); return result; public static <T2> choose(list<t2> lst) { int i = // random number < lst.size return lst.get(i);

Generic methods solve the problem class Utils { public static <T1 extends Number> double sumlist(list<t1> lst){ double result = 0.0; for (Number n : lst) { // T1 also works result += n.doublevalue(); return result; public static <T2> T2 choose(list<t2> lst) { int i = // random number < lst.size return lst.get(i); Insert a type parameter declaration in the method header! What if T1 and T2 had the same name?

Generic methods solve the problem class Utils { public static <T1 extends Number> double sumlist(list<t1> lst){ double result = 0.0; for (Number n : lst) { // T1 also works result += n.doublevalue(); return result; public static <T2> T2 choose(list<t2> lst) { int i = // random number < lst.size return lst.get(i); Scope of T1 is the body of sumlist Scope of T2 is the body of choose

Using generics in methods Instance methods can use type parameters of the class Instance methods and static methods can have their own type parameters Generic methods Callers to generic methods need not explicitly instantiate the methods type parameters Compiler usually figures it out for you Type inference

More examples <T extends Comparable<T>> T max(collection<t> c) { <T extends Comparable<T>> void sort(list<t> list) { // use list.get() and T s compareto (This one works, but we will make it even more useful later by adding more bounds.) <T> void copyto(list<t> dst, List<T> src) { for (T t : src) dst.add(t);

Outline Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Digression: Java s unsoundness(es) Java realities: type erasure

Generics and Subtyping

Generics and subtyping Number Integer List<Number>? List<Integer> Integer is a subtype of Number Is List<Integer> a subtype of List<Number>? Use subtyping rules (stronger, weaker) to find out

List<Number> and List<Integer> interface List<T> { boolean add(t elt); T get(int index); Number Integer List<Number>? List<Integer> List<Integer>? List<Number> So type List<Number> has: boolean add(number elt); Number get(int index); So type List<Integer> has: boolean add(integer elt); Integer get(int index); - Subtype needs stronger spec than super - Stronger method spec has: - weaker precondition - stronger postcondition Java subtyping is invariant with respect to generics Neither List<Number> nor List<Integer> subtype of other Not covariant and not contravariant

Generics and subtyping If T2 and T3 are different types, then for all Foo, Foo<T2> is not a subtype of Foo<T3> Previous example shows why: Observer method prevents one direction Mutator/producer method prevents the other direction If our types have only observers or only mutators, then one direction of subtyping would be sound Java s type system is not expressive enough to allow this

Read-only allows covariance (in theory) interface ReadOnlyList<T> { T get(int index); Number Integer ROList<Number> ROList<Integer> Type ReadOnlyList<Number> has method: Number get(int index); Type ReadOnlyList<Integer> has method: Integer get(int index); So covariant subtyping would be correct: ROList<Integer> is a subtype of ROList<Number> Covariant = type of ROList<T> changes the same way T changes The Java type system conservatively disallows this subtyping - Subtype method needs: - weaker pre - stronger post

Write-only allows contravariance (in theory) interface WriteOnlyList<T> { boolean add(t elt); Number Integer WOList<Integer> WOList<Number> Type WriteOnlyList<Number> has method: boolean add(number elt); Type WriteOnlyList<Integer> has method: boolean add(integer elt); So contravariant subtyping would be correct: WOList<Number> is a subtype of WOList<Integer> Contravariant = type of ROList<T> changes opposite to T The Java type system conservatively disallows this subtyping - Subtype method needs: - weaker pre - stronger post

Generic types and subtyping List<Integer> and List<Number> are not subtype-related Generic types can have subtyping relationships Example: If HeftyBag extends Bag, then HeftyBag<Integer> is a subtype of Bag<Integer> HeftyBag<Number> is a subtype of Bag<Number> HeftyBag<String> is a subtype of Bag<String>

Outline Basics of generic types for classes and interfaces Basics of bounding generics Generic methods [not just using type parameters of class] Generics and subtyping Using bounds for more flexible subtyping Using wildcards for more convenient bounds Digression: Java s unsoundness(es) Java realities: type erasure Stay tuned for lec15!

Announcements

Announcements Quiz 5 is due Thursday Homework 6 due Thursday Midterm grades and feedback will be out this evening Thank you for coming to class today! J