Taming Wildcards in Javas Type System Ross Tate Alan Leung Sorin Lerner University of California, San Diego.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Improving structure with inheritance Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
Generic programming in Java
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Improving structure with inheritance
Java Generics.
Confined Types Encapsulation and modularity Seminar November, 2005 presented by: Guy Gueta.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor John Altidor Taming the Wildcards: Combining Definition- and Use-Site Variance.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Featherweight Generic Ownership Alex Potanin, James Noble Victoria University of Wellington Dave Clarke CWI, Netherlands Robert Biddle Carlton University.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Polymorphism & Methods COMP206 Geoff Holmes & Bernhard Pfahringer (these slides: lots of input from Mark Hall) poly + morphos (greek) “many forms”
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Getting F-Bounded Polymorphism into Shape Ben Greenman, Fabian Muehlboeck, and Ross Tate Cornell University.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
CSE 341 Section 10 Subtyping, Review, and The Future.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Sections Inheritance and Abstract Classes
Chapter 11 Inheritance and Polymorphism
Chapter 20 Generic Classes and Methods
Generics, Lambdas, Reflections
INF3110: Exercises Part 6 Object Orientation Comments and solutions
Continuing Chapter 11 Inheritance and Polymorphism
Winter 2018 CMPE212 11/12/2018 CMPE212 – Stuff…
Chapter 9 Inheritance and Polymorphism
User-Oriented Language Design
Chapter 19 Generics Dr. Clincy - Lecture.
Lecture 19 - Inheritance (Contd).
Generic programming in Java
CISC124 Assignment 4 on Inheritance due today at 7pm.
Generics, Lambdas, Reflections
Inheritance and Polymorphism
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 19 Generics.
Presentation transcript:

Taming Wildcards in Javas Type System Ross Tate Alan Leung Sorin Lerner University of California, San Diego

Why Use Wildcards? long average(List nums) { long sum = 0; for (Object num : nums) sum += ((Number)num).longValue(); return sum / nums.size(); } Runtime Type Cast long average(List Number nums) { long sum = 0; for (Number num : nums) sum += num.longValue(); return sum / nums.size(); } What about List Integer ? P extends Number long average(List P nums) { long sum = 0; for (Number num : nums) sum += num.longValue(); return sum / nums.size(); } Used only once long average(List ? extends Number nums) { long sum = 0; for (Number num : nums) sum += num.longValue(); return sum / nums.size(); } Not covariant Need use-site variance

Why Use Wildcards? P void addSuperclasses(Class P c, List ? super Class ? super P list) { Class ? super P sup = c.getSuperclass(); if (sup != null) { list.add(sup); addSuperclasses(sup, list); } Inexpressible with polymorphism Untypeable with use-site variance X : X : > P. Class X

Open Problems with Wildcards No known subtyping algorithm Subtyping rules are overly restrictive Join algorithm is incomplete Type checker is non-deterministic Type checker can affect program semantics Solved

Broken Subtyping Algorithm class C P extends D D ? super C L P {} C X < : D ? super C X class Numbers P extends Number extends ArrayList P {} List Numbers ? < : List ? extends List ? extends Number javac Stack Overflow Java No javac No Java Yes

False Negatives List ? extends List ? extends Number List ? extends List ? List ? extends ArrayList ? List ? extends Numbers ? List Numbers ? class Numbers P extends Number extends ArrayList P {} extends Number direct supertype extends Number

Our Algorithm List Numbers ? < : List ? extends List ? extends Number Numbers ? < : List ? extends Number Numbers X < : List ? extends Number List X < : List ? extends Number X < : Number class Numbers P extends Number extends ArrayList P {} X < : Number Context Sound & Complete Instantiation Inheritance Opening Instantiation Assumption (if it terminates)

Non-Termination C X < : D ? super C X D D ? super C L X < : D ? super C X C X < : D ? super C L X D D ? super C L X < : D ? super C L X C L X < : D ? super C L X class C P extends D D ? super C L P {} Inheritance Instantiation Infinite Proof of Subtyping Infinite Proof of Subtyping

Restrictions Inheritance Restriction No use of ? super in the inheritance hierarchy Parameter Restriction When constraining type parameters, ? super may only be used at covariant locations class C P extends D D ? super C L P {} P extends List List ? super C L P Termination Guaranteed

Survey Wildcards in InheritanceWildcards in Constraints Only Unconstrained Wildcards # of Superclass Declarations 9.2 Million Lines of Code Analyzed No Violations of Our Restrictions No Violations of Our Restrictions All at covariant locations

Summary of Contributions Sound and Complete Subtyping Algorithm given practical language restrictions Theory of Existential Types design of implicit constraints Techniques for Type-Argument Inference lazy joins for wildcards Fixes to the Type System improved equivalence and intersections Compatible Extensions to Java mixing use-site and declaration-site variance