301AA - Advanced Programming [AP-2017]

Similar documents
301AA - Advanced Programming [AP-2017]

Parametric polymorphism and Generics

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

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

CSE 331 Software Design & Implementation

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. 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

Polymorphism (generics)

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

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

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017]

Exercise 8 Parametric polymorphism November 18, 2016

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

Java Generics -- an introduction. Based on

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

CSE 331. Generics (Parametric Polymorphism)

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

CSCE 314 Programming Languages

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

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

Exercise 8 Parametric polymorphism November 17, 2017

JAVA V Assertions Java, winter semester

Generics with Type Bounds

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

C# Generics. Object Oriented Programming (236703) Winter

Software Engineering. Prof. Agostino Poggi

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

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

JAVA V Source files Java, winter semester

Generics method and class definitions which involve type parameters.

Advanced Programming Generics Collections

generic programming alberto ferrari university of parma

Photo credit: Andrew Kennedy JAVA GENERICS

The collections interfaces

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

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

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Java Generics Java, summer semester

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

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

Primitive Java Generic Class

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

Generic programming in OO Languages

Introducing Generics

Java Design Goals. Lecture 32: Java. Java Original implementations slow! Exceptions & Subtyping. - void method readfiles() throws IOException {...}!

Advances in Programming Languages: Type accuracy

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

COMP6700/2140 Generic Methods

Java 5 New Language Features

301AA - Advanced Programming [AP-2017]

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

Binghamton University. CS-140 Fall Dynamic Types

Compiler Construction 2016/2017 Polymorphic Types and Generics

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Generics, I/O & Regular Expressions Maria Zontak

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

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

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

The Hack programming language:

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

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

COP 3330 Final Exam Review

Java Wildcards Meet Definition-Site Variance

CS108, Stanford Handout #8. Java Generics

Compiler Construction 2009/2010 Polymorphic Types and Generics

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

Harvard School of Engineering and Applied Sciences Computer Science 152

Refactoring Java Generics by Inferring Wildcards, In Practice

PARAMETRIC POLYMORPHISM

Comp215: Covariance and Contravariance

Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Collections, Maps and Generics

Midterm Exam CS 251, Intermediate Programming October 8, 2014

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

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

PIC 20A Collections and Data Structures

Exercise 9 Parametric Polymorphism and Information Hiding November 24, 2017

CS-202 Introduction to Object Oriented Programming

#Students per correct answers

CS558 Programming Languages

Programming Languages and Techniques (CIS120)

Advanced Topics in Java and on Security in Systems Effective Generics. Eirik Eltvik 26 th of February 2007

9 A Preview of.net 2.0

Announcements. Written Assignment 2 Due Monday at 5:00PM. Midterm next Wednesday in class, 11:00 1:00. Midterm review session next Monday in class.

Javarifier: inference of reference immutability

Problems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes

23. Generics and Adapters

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Transcription:

301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-14: Java Generics

Outline Java generics Type bounds Generics and subtyping Covariance, contravariance in Java and other languages Subtyping and arrays in Java Wildcards Type erasure LimitaQons of generics 2

ClassificaQon of Polymorphism Universal Coercion Parametric Implicit Explicit Bounded Polymorphism Inclusion Covariant Invariant Contravariant Ad hoc Overriding Overloading

Java Generics Explicit Parametric Polymorphism Classes, Interfaces, Methods can have type parameters The type parameters can be used arbitrarly in the definiqon They can be instanqated by providing arbitrary (reference) type arguments We discuss only a few issues about Java generics interface List<E> { boolean add(e n); E get(int index); } List<Integer> List<Number> List<String> List<List<String>> Tutorials on Java generics: https://docs.oracle.com/javase/tutorial/java/generics/index.html! http://thegreyblog.blogspot.it/2011/03/!!java-generics-tutorial-part-i-basics.html! 4

Generic methods Methods can use the type parameters of the class where they are defined, if any They can also introduce their own type parameters public static <T> T getfirst(list<t> list)!! InvocaQons of generic methods must instanqate all type parameters, either explicitly or implicitly A form of type inference 5

Bounded Type Parameters interface List<E extends Number> { void m(e arg) { arg.asint(); // OK, Number and its subtypes // support asint() } } Only classes implemenqng Number can be used as type arguments Method defined in the bound (Number) can be invoked on objects of the type parameter 6

Type Bounds <TypeVar extends SuperType> upper bound; SuperType and any of its subtype are ok. <TypeVar extends ClassA & InterfaceB & InterfaceC & > Mul4ple upper bounds <TypeVar super SubType> lower bound; SubType and any of its supertype are ok Type bounds for methods guarantee that the type argument supports the operaqons used in the method body Unlike C++ where overloading is resolved and can fail ader instanqaqng a template, in Java type checking ensures that overloading will succeed 7

A generic algorithm with type bounds public static <T> int countgreaterthan(t[] anarray, T elem) {! int count = 0;! for (T e : anarray)! if (e > elem) // compiler error! ++count;! return count;! }! public interface Comparable<T> {!// classes implementing! public int compareto(t o);!// Comparable provide a! }!!// default way to compare their objects! public static <T extends Comparable<T>>!!!!!int countgreaterthan(t[] anarray, T elem) {! int count = 0;! for (T e : anarray)! }! if (e.compareto(elem) > 0) ++count;! return count;!!// ok, it compiles! 8

Generics and subtyping Number Integer List<Number>? List<Integer> Integer is subtype of Number! Is List<Integer> subtype of List<Number>? NO! 9

What are Java rules? Given two concrete types A and B, MyClass<A> has no rela9onship to MyClass<B>, regardless of whether or not A and B are related. Formally: subtyping in Java is invariant for generic classes. Note: The common parent of MyClass<A> and MyClass<B> is MyClass<?>: the wildcard? Will be discussed later. On the other hand, as expected, if A extends B and they are generic classes, for each type C we have that A<C> extends B<C>. Thus, for example, ArrayList<Integer> is subtype of List<Integer> 10

List<Number> e List<Integer> interface List<T> { boolean add(t elt); T get(int index); } type List<Number> has: boolean add(number elt); Number get(int index); List<Integer> lisint = new ;! List<Number> lisnum = new ;! lisnum = lisint;!//???! lisnum.add(new Number( ));//no! listint = lisnum;!//???! Integer n = lisint.get(0); //no! Number type List<Integer> has: boolean add(integer elt); Integer get(int index); Integer Is the Subs9tu9on Principle saqsfied in either direcqon? Thus List<Number> is neither a supertype nor a subtype of List<Integer>: Java rules are adequate here 11

But in more specific situaqons interface List<T> { T get(int index); } type List<Number>: Number get(int index); Number Integer type List<Integer>: Integer get(int index); A covariant no9on of subtyping would be safe: List<Integer> can be subtype of List<Number> Not in Java In general: covariance is safe if the type is read-only 12

Viceversa contravariance! interface List<T> { boolean add(t elt); } type List<Number>: boolean add(number elt); Number Integer type List<Integer>: boolean add(integer elt); A contravariant no9on of subtyping would be safe: List<Number> can be a subtype of List<Integer> But Java.. In general: contravariance is safe if the type is write-only 13

Generics and subtypes in C# In C#, the type parameter of a generic class can be annotated out (covariant) or in (contravariant), otherwise it is invariant. Examples: Ienumerator is covariant, because the only method returns an enumerator, which accesses the collecqon in read-only public interface IEnumerable<out T> : [ ] {!!public [ ]IEnumerator<out T> GetEnumerator ();! }! IComparable is contravariant, because the only method has an argument of type T public interface IComparable<in T> {!!public int CompareTo (T other);! }! 14

Co- and Contra-variance in Scala Also Scala supports co/contra-variance annotaqons (- and +) for type parameters:! class VendingMachine[+A]{ }!! class GarbageCan[-A]{ }!! trait Function1[-T, +R] extends AnyRef! { def apply(v1: T): R }! http://blog.kamkor.me/covariance-and-contravariance-in-scala/! 15

A digression: Java arrays Arrays are like built-in containers Let Type1 be a subtype of Type2. How are Type1[] e Type2[]related? Consider the following generic class, mimicking arrays: class Array<T> {! public T get(int i) { op }! public T set(t newval, int i) { op }! }! According with Java rules, Array<Type1> and Array<Type2> are not related by subtyping 16

But instead In Java, if Type1 is a subtype of Type2, then Type1[] is a subtype of Type2[]. Thus Java arrays are covariant. Java (and also C#,.NET) fixed this rule before the introducqon of generics. Why? Think to void sort(object[] o);! Not convenient to change it because of retrocompability consideraqons. Covariance works fine, provided you do not change the content of an array 17

A safe use of covariance in arrays LibraryHolding void maybeswap(libraryholding[] arr) { Book if(arr[17].duedate() < arr[34].duedate()) // swap arr[17] and arr[34] } CD // client Book[] books = ; maybeswap(books); // uses covariance of arrays 18

But inserqon causes failure void replace17(libraryholding[] arr, LibraryHolding h) { arr[17] = h; } LibraryHolding Book CD // the client Book[] books = ; LibraryHolding thewall = new CD("Pink Floyd", "The Wall", ); replace17(books, thewall); Book b = books[17]; // contains a CD b.getchapters(); // problem!! 19

Java s choices For each reference variable, the dynamic type (type of the object referred by it) must be a subtype of the sta9c one (type of declaraqon). Violated for Book b Java design choices: The dynamic type of an array is known at runqme (Book[], or beber [Lmypackage.Book; in JVM type syntax) Every Java array-update includes run-9me check Assigning an object of a supertype to an array throws an ArrayStoreException Thus replace17 throws an excepqon 20

A simpler example Apple[] apples = new Apple[1];! Fruit[] fruits = apples;! fruits[0] = new Strawberry();! // JVM throws ArrayStoreException! 21

A serious consequence Every Java array-update includes run-qme check, but Generic types are not present at runqme due to type erasure, thus Arrays of generics are not supported in Java In fact they would cause type errors not detectable at runqme, breaking Java strong type safety List<String>[] lsa = new List<String>[10]; // illegal! Object[] oa = lsa; // OK by covariance of arrays! List<Integer> li = new ArrayList<Integer>();! li.add(new Integer(3));! oa[0] = li; // should throw ArrayStoreExeception,!!// but JVM only sees oa[0]:list = li:arraylist! String s = lsa[0].get(0); // type error!!! 22

Wildcards for covariance Invariance of generic classes is restricqve Wildcards can alleviate the problem What is a general enough type for addall? interface Set<E> { // Adds to this all elements of c // (not already in this) void addall(??? c); } void addall(set<e> c) // and List<E>? void addall(collection<e> c) // and collecqons of T <: E? void addall(collection<? extends E> c); // ok 23

Syntax of wildcards: Wildcards, for both co- and contra-variance? extends Type, denotes an unknown subtype of Type?, shorthand for? extends Object? super Type, denotes an unknown supertype of Type wildcard = anonymous variable? Unknown type Wildcard are used when a type is used exactly once, and the name is unknown They are used for use-site variance (not declara9on-site variance) 24

The PECS principle : Producer Extends, Consumer Super When should wildcards be used? Use? extends T when you want to get values (from a producer): supports covariance Use? super T when you want to insert values (in a consumer): supports contravariance Do not use? (T is enough) when you both obtain and produce values. Example: <T> void copy(list<? super T> dst, List<? extends T> src); 25

What about type safety? Arrays covariance: Apple[] apples = new Apple[1];! Fruit[] fruits = apples;! fruits[0] = new Strawberry();! // JVM throws ArrayStoreException! Covariance with wildcards: List<Apple> apples = new ArrayList<Apple>();! List<? extends Fruit> fruits = apples;! fruits.add(new Strawberry());!!// compile-time error!!!! 26

The price to pay with wildcards A wildcard type is anonymous/unknown, and almost nothing can be done: List<Apple> apples = new ArrayList<Apple>();! List<? extends Fruit> fruits = apples; //covariance! fruits.add(new Strawberry()); // compile-time error! OK! Fruits f = apples.get(0); // OK! fruits.add(new Apple()); // compile-time error???! fruits.add(null); //ok, the only thing you can add!! List<Fruit> fruits = new ArrayList<Fruits>();! List<? super Apples> apples = fruits; //contravariance! fruits.add(new Apple()); // OK! fruits.add(new FujiApple()); // OK! fruits.add(new Fruit()); // compile-time error, OK! Fruits f = apples.get(0); // compile-time error???! Object o = apples.get(0); //ok, the only way to get! 27

Type erasure All type parameters of generic types are transformed to Object or to their first bound ader compilaqon Main Reason: backward compaqbility with legacy code Thus at run-qme, all the instances of the same generic type have the same type List<String> lst1 = new ArrayList<String>(); List<Integer> lst2 = new ArrayList<Integer>(); lst1.getclass() == lst2.getclass() // true 28

LimitaQons of Java Generics Cannot InstanQate Generic Types with PrimiQve Types ArrayList<int> a = //does not compile Cannot Create Instances of Type Parameters Cannot Declare StaQc Fields Whose Types are Type Parameters public class C<T>{ public static T local; }! Cannot Use Casts or instanceof With Parameterized Types (list instanceof ArrayList<Integer>) // does not compile! (list instanceof ArrayList<?>) // ok Cannot Create Arrays of Parameterized Types Cannot Create, Catch, or Throw Objects of Parameterized Types Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type public class Example { // does not copile! public void print(set<string> strset) { }! public void print(set<integer> intset) { } }! 29