in Javas Type System Ross Tate Alan Leung Sorin Lerner University of California San Diego Why Use Wildcards long averageList nums long sum 0 for Object num nums sum NumbernumlongValue ID: 278498
Download Presentation The PPT/PDF document "Taming Wildcards" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Taming Wildcardsin Java’s Type System
Ross TateAlan LeungSorin Lerner
University of California, San DiegoSlide2
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 varianceSlide3
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〉Slide4
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 semanticsSolvedSlide5
Subtyping WildcardsSlide6
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
YesSlide7
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 Numberextends Number
direct supertype
direct supertype
direct supertype
extends NumberSlide8
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)Slide9
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
Inheritance
Instantiation
Instantiation
Infinite Proof
of Subtyping
ContrivedSlide10
Restrictions
Inheritance RestrictionNo 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〉〉〉〉〉Contrived
ContrivedTermination GuaranteedSlide11
Survey
Wildcards in InheritanceWildcards in Constraints
Only Unconstrained
Wildcards
0
100000
10000
1000
# of Superclass
Declarations9.2 Million Lines of Code Analyzed
No Violations of
Our Restrictions
All at covariant locations
Slide12
Summary of Contributions
Sound and Complete Subtyping Algorithmgiven practical language restrictionsTheory of Existential Typesdesign of implicit constraintsTechniques for Type-Argument Inference
lazy joins for wildcards
Fixes to the Type System
improved equivalence and intersectionsCompatible Extensions to Javamixing use-site and declaration-site variance