Wildcards - PowerPoint PPT Presentation

About This Presentation
Title:

Wildcards

Description:

Clashes with object-oriented (subtype) abstraction. Wildcards. Mediate the two modes of abstraction. Address deep type-theoretic issues. The Wildcards Project ... – PowerPoint PPT presentation

Number of Views:314
Avg rating:3.0/5.0
Slides: 56
Provided by: gaf3
Category:

less

Transcript and Presenter's Notes

Title: Wildcards


1
  • Wildcards
  • in the Java Programming Language
  • Neal Gafter
  • with thanks to
  • Mads Torgersen, University of Aarhus

2
Wildcards
  • 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

3
The 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

4
Project 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)

5
Java Genericity
interface List extends Collection void
add(Object element) Object get(int index)
...
6
Java 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)

7
Java 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)

8
Java 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)
9
Static typecheck
List numbers ... numbers.add(Seven) Number n
(Number)numbers.get(0)
ListltNumbergt numbers ... numbers.add(Seven) N
umber n numbers.get(0)
10
Static 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)
11
Static 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)
12
Object-Oriented Abstraction
  • Common view on common properties

Collection
Set
List
13
Object-Oriented Abstraction
  • Common view on common properties

Collection
Set
List
List l ... Collection c l
14
Object-Oriented Abstraction
  • Pointwise subtyping

CollectionltNumbergt
SetltNumbergt
ListltNumbergt
ListltNumbergt nl ... CollectionltNumbergt nc nl
15
Parametric Abstraction
  • What is the common view?

?
ListltStringgt
ListltNumbergt
16
In 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)
17
In 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)
18
The Array approach
  • Runtime store check on every assignment

Object
String
Number
Number numbers ... Object things
numbers things0 Seven Number n
numbers0
19
The Array approach
  • Runtime store check on every assignment

Object
String
Number
Runtime type error!
Number numbers ... Object things
numbers Things0 Seven Number n
numbers0
20
Can 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)
21
Can 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)
22
Can 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)
23
Can 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)
24
The raw types approach
List
?
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... List things
numbers things.add(Seven) Number n
numbers.get(0)
25
The 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!
26
The 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)
27
The 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)
28
The GJ approach
Object
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Object things
numbers things.add(Seven) Object o
things.get(0)
29
The GJ approach
Object
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Object things
numbers things.add(Seven) Object o
things.get(0)
Compile time error!
30
The 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!
31
The 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!
32
Requirements 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

33
Wildcards
  • List of something

?
Listlt gt
ListltStringgt
ListltNumbergt
34
Wildcards
  • List of something

?
Listlt gt
ListltStringgt
ListltNumbergt
35
Wildcards
  • List of something

Listlt?gt
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Listlt?gt things
numbers things.add(Seven) Object o
things.get(0)
36
Wildcards
  • List of something

Listlt?gt
ListltStringgt
ListltNumbergt
ListltNumbergt numbers ... Listlt?gt things
numbers things.add(Seven) Object o
things.get(0)
37
Wildcards in Collections
package java.util public interface
CollectionltEgt boolean containsAll(Collectionlt?
gt c) boolean removeAll(Collectionlt?gt c)
...
38
Can we do better?
  • Integer and Float are related

Listlt?gt
ListltFloatgt
ListltIntegergt
39
Bounded Wildcards
  • Numbers come out

Listlt? extends Numbergt
ListltFloatgt
ListltIntegergt
ListltIntegergt ints ... Listlt? extends Numbergt
numbers ints Number n numbers.get(0) numbers
.add(.7F)
40
Bounded Wildcards
  • Adding still prohibited

Listlt? extends Numbergt
ListltFloatgt
ListltIntegergt
ListltIntegergt ints ... Listlt? extends Numbergt
numbers ints Number n numbers.get(0) numbers
.add(.7F)
41
Extends-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) ...
42
What can we do about adding?
  • Adding still prohibited

Listlt? extends Numbergt
ListltNumbergt
ListltIntegergt
43
Super-bounded Wildcards
Listlt? super Integergt
ListltNumbergt
ListltIntegergt
ListltNumbergt numbers ... Listlt? super Integergt
ints numbers ints.add(7) Integer i
ints.get(0)
44
Super-bounded Wildcards
  • Only Objects come out

Listlt? super Integergt
ListltNumbergt
ListltIntegergt
ListltNumbergt numbers ... Listlt? super Integergt
ints numbers ints.add(7) Integer i
ints.get(0)
45
TreeSet 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) ...
46
Collections 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) ...
47
Subtyping with wildcards
Listlt?gt
Listlt? extends Numbergt
Listlt? extends Integergt
ListltNumbergt
ListltIntegergt
48
Subtyping with wildcards
Listlt?gt
Listlt? super Integergt
Listlt? super Numbergt
ListltIntegergt
ListltNumbergt
49
Type inference
  • Better types to choose from

ltTgt T choose(T fst, T snd) ...
ListltNumbergt numbers ... SetltStringgt strings
... Collectionlt?gt c choose(numbers,strings)
50
Type inference
  • Built-in condition expression

Boolean b ... ListltNumbergt numbers
... SetltStringgt strings ... Collectionlt?gt c
b ? numbers strings
51
Capture
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) ...
52
Capture
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?
53
Capture
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)
54
Capture
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
55
Wildcard 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
Write a Comment
User Comments (0)
About PowerShow.com