Title: Wildcards
1- Wildcards
-
- in the Java Programming Language
- Neal Gafter
- with thanks to
- Mads Torgersen, University of Aarhus
2Wildcards
- Genericity
- Improves type system expressiveness
- Based on parametric abstraction
- The abstraction gap
- Clashes with object-oriented (subtype)
abstraction - Wildcards
- Mediate the two modes of abstraction
- Address deep type-theoretic issues
3The Wildcards Project
- University of Aarhus
- Mads Torgersen, Erik Ernst, Peter von der Ahé and
Christian Plesner Hansen - Sun Microsystems
- Gilad Bracha and Neal Gafter
- Based on previous research by
- Mads Torgersen Kresten Krab Thorup
- Mirko Viroli Atsushi Igarashi
4Project Results
- Design of the wildcard mechanism
- Mediation of the abstraction gap
- Integration with other language features
- Implementation
- javac, the Java compiler
- Java Platform APIs
- Part of JDK1.5 (Tiger)
5Java Genericity
interface List extends Collection void
add(Object element) Object get(int index)
...
6Java Genericity
interface List extends Collection void
add(Object element) Object get(int index)
...
List numbers new ArrayList() numbers.add(new
Integer(7)) Number n (Number)numbers.get(0)
7Java Genericity
interface ListltTgt extends CollectionltTgt void
add(T element) T get(int index) ...
List numbers new ArrayList() numbers.add(new
Integer(7)) Number n (Number)numbers.get(0)
8Java Genericity
interface ListltTgt extends CollectionltTgt void
add(T element) T get(int index) ...
ListltNumbergt numbers new
ArrayListltNumbergt() numbers.add(new
Integer(7)) Number n numbers.get(0)
9Static typecheck
List numbers ... numbers.add(Seven) Number n
(Number)numbers.get(0)
ListltNumbergt numbers ... numbers.add(Seven) N
umber n numbers.get(0)
10Static typecheck
Runtime type error!
List numbers ... numbers.add(Seven) Number n
(Number)numbers.get(0)
ListltNumbergt numbers ... numbers.add(Seven) N
umber n numbers.get(0)
11Static typecheck
List numbers ... numbers.add(Seven) Number n
(Number)numbers.get(0)
Compile time error
ListltNumbergt numbers ... numbers.add(Seven) N
umber n numbers.get(0)
12Object-Oriented Abstraction
- Common view on common properties
Collection
Set
List
13Object-Oriented Abstraction
- Common view on common properties
Collection
Set
List
List l ... Collection c l
14Object-Oriented Abstraction
CollectionltNumbergt
SetltNumbergt
ListltNumbergt
ListltNumbergt nl ... CollectionltNumbergt nc nl
15Parametric Abstraction
?
ListltStringgt
ListltNumbergt
16In the old days...
- ...any List was just a List
List
List
List
List numbers ... List things
numbers things.add(Seven) Number n
(Number)number.get(0)
17In the old days...
- ...any List was just a List
List
List
List
List numbers ... List things
numbers things.add(Seven) Number n
(Number)number.get(0)
18The Array approach
- Runtime store check on every assignment
Object
String
Number
Number numbers ... Object things
numbers things0 Seven Number n
numbers0
19The Array approach
- Runtime store check on every assignment
Object
String
Number
Runtime type error!
Number numbers ... Object things
numbers Things0 Seven Number n
numbers0
20Can we do the same for List?
ListltObjectgt
?
ListltStringgt
ListltNumbergt
Runtime type error?
ListltNumbergt numbers ... ListltObjectgt things
numbers things.add(Seven) Number n
numbers.get(0)
21Can we do the same for List?
- Erasure No type argument info at runtime
ListltObjectgt
?
ListltStringgt
ListltNumbergt
Runtime type error?
ListltNumbergt numbers ... ListltObjectgt things
numbers things.add(Seven) Number n
numbers.get(0)
22Can we do the same for List?
- Erasure No type argument info at runtime
ListltObjectgt
ListltStringgt
ListltNumbergt
Runtime type error!
ListltNumbergt numbers ... ListltObjectgt things
numbers things.add(Seven) Number n
numbers.get(0)
23Can we do the same for List?
- No type argument info at runtime
ListltObjectgt
ListltStringgt
ListltNumbergt
Compile time error!
ListltNumbergt numbers ... ListltObjectgt things
numbers things.add(Seven) Number n
numbers.get(0)
24The raw types approach
List
?
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... List things
numbers things.add(Seven) Number n
numbers.get(0)
25The raw types approach
- Compile time type check undermined
List
?
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... List things
numbers things.add(Seven) Number n
numbers.get(0)
Runtime type error!
26The raw types approach
- Compile time type check undermined
List
ListltStringgt
ListltNumbergt
Compile time warning
ListltNumbergt numbers ... List things
numbers things.add(Seven) Number n
numbers.get(0)
27The raw types approach
- Compile time type check undermined
List
ListltStringgt
ListltNumbergt
Compile time warning
ListltNumbergt numbers ... List things
numbers things.add(Seven) Number n
numbers.get(0)
28The GJ approach
Object
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Object things
numbers things.add(Seven) Object o
things.get(0)
29The GJ approach
Object
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Object things
numbers things.add(Seven) Object o
things.get(0)
Compile time error!
30The GJ approach
- Cannot use common interface
Object
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Object things
numbers things.add(Seven) Object o
things.get(0)
Compile time error!
31The GJ approach
- Cannot use common interface
Object
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Object things
numbers things.add(Seven) Object o
things.get(0)
Compile time error!
32Requirements for a common view
- It should be some kind of List
- so that we can safely get elements out of it
- It should prevent insertion of wrong elements
- to avoid heap pollution
- It should do so at compile time
- because we have no runtime type argument info
- It must prevent insertion of any elements
- because it is a share view of all Lists
33Wildcards
?
Listlt gt
ListltStringgt
ListltNumbergt
34Wildcards
?
Listlt gt
ListltStringgt
ListltNumbergt
35Wildcards
Listlt?gt
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Listlt?gt things
numbers things.add(Seven) Object o
things.get(0)
36Wildcards
Listlt?gt
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Listlt?gt things
numbers things.add(Seven) Object o
things.get(0)
37Wildcards in Collections
package java.util public interface
CollectionltEgt boolean containsAll(Collectionlt?
gt c) boolean removeAll(Collectionlt?gt c)
...
38Can we do better?
- Integer and Float are related
Listlt?gt
ListltFloatgt
ListltIntegergt
39Bounded Wildcards
Listlt? extends Numbergt
ListltFloatgt
ListltIntegergt
ListltIntegergt ints ... Listlt? extends Numbergt
numbers ints Number n numbers.get(0) numbers
.add(.7F)
40Bounded Wildcards
Listlt? extends Numbergt
ListltFloatgt
ListltIntegergt
ListltIntegergt ints ... Listlt? extends Numbergt
numbers ints Number n numbers.get(0) numbers
.add(.7F)
41Extends-bounds in Collections
package java.util public interface
CollectionltEgt boolean containsAll(Collectionlt?
gt c) boolean removeAll(Collectionlt?gt c)
boolean addAll(Collectionlt? extends Egt c) ...
42What can we do about adding?
Listlt? extends Numbergt
ListltNumbergt
ListltIntegergt
43Super-bounded Wildcards
Listlt? super Integergt
ListltNumbergt
ListltIntegergt
ListltNumbergt numbers ... Listlt? super Integergt
ints numbers ints.add(7) Integer i
ints.get(0)
44Super-bounded Wildcards
Listlt? super Integergt
ListltNumbergt
ListltIntegergt
ListltNumbergt numbers ... Listlt? super Integergt
ints numbers ints.add(7) Integer i
ints.get(0)
45TreeSet Constructors
package java.util public class TreeSetltEgt
implements OrderedSetltEgt public
TreeSet(OrderedSetltEgt c) public
TreeSet(Collectionlt? extends Egt c) public
TreeSet(Comparatorlt? super Egt c) ...
46Collections utility methods
T
package java.util public class Collections
public static void reverse(Listlt?gt list)
public static void shuffle(Listlt?gt list)
public static ltTgt void fill( Listlt? super
Tgt list, T obj) public static ltTgt void copy(
Listlt? super Tgt dest, Listlt? extends
Tgt src) ...
47Subtyping with wildcards
Listlt?gt
Listlt? extends Numbergt
Listlt? extends Integergt
ListltNumbergt
ListltIntegergt
48Subtyping with wildcards
Listlt?gt
Listlt? super Integergt
Listlt? super Numbergt
ListltIntegergt
ListltNumbergt
49Type inference
- Better types to choose from
ltTgt T choose(T fst, T snd) ...
ListltNumbergt numbers ... SetltStringgt strings
... Collectionlt?gt c choose(numbers,strings)
50Type inference
- Built-in condition expression
Boolean b ... ListltNumbergt numbers
... SetltStringgt strings ... Collectionlt?gt c
b ? numbers strings
51Capture
package java.util public class Collections
public static void reverse(Listlt?gt list)
public static void shuffle(Listlt?gt list)
public static ltTgt void fill( Listlt? super
Tgt list, T obj) public static ltTgt void copy(
Listlt? super Tgt dest, Listlt? extends
Tgt src) ...
52Capture
package java.util public class Collections
public static void reverse(Listlt?gt list)
public static void shuffle(Listlt?gt list)
public static ltTgt void fill( Listlt? super
Tgt list, T obj) public static ltTgt void copy(
Listlt? super Tgt dest, Listlt? extends
Tgt src) ...
How is reverse() implemented?
53Capture
public static void reverse(Listlt?gt list)
rev(list) private static ltTgt void
rev(ListltTgt list) for (int i 0 i lt
list.length/2 i) int j list.length -
i - 1 T tmp list.get(i)
list.set(i, list.get(j)) list.set(j,
tmp)
54Capture
public static void reverse(Listlt?gt list)
rev(list) private static ltTgt void
rev(ListltTgt list) for (int i 0 i lt
list.length/2 i) int j list.length -
i - 1 T tmp list.get(i)
list.set(i, list.get(j)) list.set(j,
tmp)
Capture
55Wildcard conclusions
- Bridges the gap between object-oriented and
polymorphic abstraction - Simpler and more precise signatures
- Better type inference
- All over the JDK 5 APIs
- http//java.sun.com/j2se/1.5.0