Wildcards
Download
1 / 55

Wildcards in the Java Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus - PowerPoint PPT Presentation


  • 295 Views
  • Uploaded on

Wildcards in the Java ™ Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus. Wildcards. Genericity Improves type system expressiveness Based on parametric abstraction The abstraction gap: Clashes with object-oriented (subtype) abstraction Wildcards

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Wildcards in the Java Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus' - washington


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Wildcards

in the Java™ Programming Language

Neal Gafter

with thanks to

Mads Torgersen, University of Aarhus


Wildcards
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


The wildcards project
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


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


Java genericity
Java Genericity

interface List extends Collection {

void add(Object element);

Object get(int index);

...

}


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


Java genericity2
Java Genericity

interface List<T> extends Collection<T> {

void add(T element);

T get(int index);

...

}

List numbers = new ArrayList();

numbers.add(new Integer(7));

Number n = (Number)numbers.get(0);


Java genericity3
Java Genericity

interface List<T> extends Collection<T> {

void add(T element);

T get(int index);

...

}

List<Number> numbers

= new ArrayList<Number>();

numbers.add(new Integer(7));

Number n = numbers.get(0);


Static typecheck
Static typecheck

List numbers = ...

numbers.add(”Seven”);

Number n = (Number)numbers.get(0);

List<Number> numbers = ...

numbers.add(”Seven”);

Number n = numbers.get(0);


Static typecheck1
Static typecheck

Runtime type error!

List numbers = ...

numbers.add(”Seven”);

Number n = (Number)numbers.get(0);

List<Number> numbers = ...

numbers.add(”Seven”);

Number n = numbers.get(0);


Static typecheck2
Static typecheck

List numbers = ...

numbers.add(”Seven”);

Number n = (Number)numbers.get(0);

Compile time error

List<Number> numbers = ...

numbers.add(”Seven”);

Number n = numbers.get(0);


Object oriented abstraction
Object-Oriented Abstraction

Common view on common properties

Collection

Set

List


Object oriented abstraction1
Object-Oriented Abstraction

Common view on common properties

Collection

Set

List

List l = ...;

Collection c = l;


Object oriented abstraction2
Object-Oriented Abstraction

Pointwise subtyping

Collection<Number>

Set<Number>

List<Number>

List<Number> nl = ...;

Collection<Number> nc = nl;


Parametric abstraction
Parametric Abstraction

What is the common view?

?

List<String>

List<Number>


In the old days
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);


In the old days1

Runtime type error!

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


The array approach
The Array approach

Runtime store check on every assignment

Object[]

String[]

Number[]

Number[] numbers = ...;

Object[] things = numbers;

things[0] = ”Seven”;

Number n = numbers[0];


The array approach1
The Array approach

Runtime store check on every assignment

Object[]

String[]

Number[]

Runtime type error!

Number[] numbers = ...;

Object[] things = numbers;

Things[0] = ”Seven”;

Number n = numbers[0];


Can we do the same for list
Can we do the same for List?

List<Object>

?

List<String>

List<Number>

Runtime type error?

List<Number> numbers = ...;

List<Object> things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


Can we do the same for list1
Can we do the same for List?

Erasure: No type argument info at runtime

List<Object>

?

List<String>

List<Number>

Runtime type error?

List<Number> numbers = ...;

List<Object> things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


Can we do the same for list2
Can we do the same for List?

Erasure: No type argument info at runtime

List<Object>

List<String>

List<Number>

Runtime type error!

List<Number> numbers = ...;

List<Object> things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


Can we do the same for list3
Can we do the same for List?

No type argument info at runtime

List<Object>

List<String>

List<Number>

Compile time error!

List<Number> numbers = ...;

List<Object> things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


The raw types approach
The raw types approach

List

?

List<String>

List<Number>

List<Number> numbers = ...;

List things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


The raw types approach1
The raw types approach

Compile time type check undermined

List

?

List<String>

List<Number>

List<Number> numbers = ...;

List things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);

Runtime type error!


The raw types approach2
The raw types approach

Compile time type check undermined

List

List<String>

List<Number>

Compile time warning

List<Number> numbers = ...;

List things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


The raw types approach3
The raw types approach

Compile time type check undermined

List

List<String>

List<Number>

Compile time warning

List<Number> numbers = ...;

List things = numbers;

things.add(”Seven”);

Number n = numbers.get(0);


The gj approach
The GJ approach

Object

List<String>

List<Number>

List<Number> numbers = ...;

Object things = numbers;

things.add(”Seven”);

Object o = things.get(0);


The gj approach1
The GJ approach

Object

List<String>

List<Number>

List<Number> numbers = ...;

Object things = numbers;

things.add(”Seven”);

Object o = things.get(0);

Compile time error!


The gj approach2
The GJ approach

Cannot use common interface

Object

List<String>

List<Number>

List<Number> numbers = ...;

Object things = numbers;

things.add(”Seven”);

Object o = things.get(0);

Compile time error!


The gj approach3
The GJ approach

Cannot use common interface

Object

List<String>

List<Number>

List<Number> numbers = ...;

Object things = numbers;

things.add(”Seven”);

Object o = things.get(0);

Compile time error!


Requirements for a common view
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


Wildcards1
Wildcards

”List of something ”

?

List< >

List<String>

List<Number>


Wildcards2
Wildcards

”List of something ”

?

List< >

List<String>

List<Number>


Wildcards3
Wildcards

”List of something ”

List<?>

List<String>

List<Number>

List<Number> numbers = ...;

List<?> things = numbers;

things.add(”Seven”);

Object o = things.get(0);


Wildcards4
Wildcards

”List of something ”

List<?>

List<String>

List<Number>

List<Number> numbers = ...;

List<?> things = numbers;

things.add(”Seven”);

Object o = things.get(0);


Wildcards in collections
Wildcards in Collections

package java.util;

public interface Collection<E> {

boolean containsAll(Collection<?> c);

boolean removeAll(Collection<?> c);

...

}


Can we do better
Can we do better?

Integer and Float are related

List<?>

List<Float>

List<Integer>


Bounded wildcards
Bounded Wildcards

Numbers come out

List<? extends Number>

List<Float>

List<Integer>

List<Integer> ints = ...;

List<? extends Number> numbers = ints;

Number n = numbers.get(0);

numbers.add(.7F);


Bounded wildcards1
Bounded Wildcards

Adding still prohibited

List<? extends Number>

List<Float>

List<Integer>

List<Integer> ints = ...;

List<? extends Number> numbers = ints;

Number n = numbers.get(0);

numbers.add(.7F);


Extends bounds in collections
Extends-bounds in Collections

package java.util;

public interface Collection<E> {

boolean containsAll(Collection<?> c);

boolean removeAll(Collection<?> c);

boolean addAll(Collection<? extends E> c);

...

}


What can we do about adding
What can we do about adding?

Adding still prohibited

List<? extends Number>

List<Number>

List<Integer>


Super bounded wildcards
Super-bounded Wildcards

List<? super Integer>

List<Number>

List<Integer>

List<Number> numbers = ...;

List<? super Integer> ints = numbers;

ints.add(7);

Integer i = ints.get(0);


Super bounded wildcards1
Super-bounded Wildcards

Only Objects come out

List<? super Integer>

List<Number>

List<Integer>

List<Number> numbers = ...;

List<? super Integer> ints = numbers;

ints.add(7);

Integer i = ints.get(0);


Treeset constructors
TreeSet Constructors

package java.util;

public class TreeSet<E>

implements OrderedSet<E> {

public TreeSet(OrderedSet<E> c);

public TreeSet(Collection<? extends E> c);

public TreeSet(Comparator<? super E> c);

...

}


Collections utility methods

T

Collections utility methods

package java.util;

public class Collections {

public static void reverse(List<?> list);

public static void shuffle(List<?> list);

public static <T> void fill(

List<? super T> list, T obj);

public static <T> void copy(

List<? super T> dest,

List<? extends T> src);

...

}


Subtyping with wildcards
Subtyping with wildcards

List<?>

List<? extends Number>

List<? extends Integer>

List<Number>

List<Integer>


Subtyping with wildcards1
Subtyping with wildcards

List<?>

List<? super Integer>

List<? super Number>

List<Integer>

List<Number>


Type inference
Type inference

Better types to choose from

<T>T choose(T fst, T snd) {

...

}

List<Number> numbers = ...

Set<String> strings = ...

Collection<?> c = choose(numbers,strings)


Type inference1
Type inference

Built-in condition expression

Boolean b = ...

List<Number> numbers = ...

Set<String> strings = ...

Collection<?> c = b ? numbers : strings


Capture
Capture

package java.util;

public class Collections {

public static void reverse(List<?> list);

public static void shuffle(List<?> list);

public static <T> void fill(

List<? super T> list, T obj);

public static <T> void copy(

List<? super T> dest,

List<? extends T> src);

...

}


Capture1
Capture

package java.util;

public class Collections {

public static void reverse(List<?> list);

public static void shuffle(List<?> list);

public static <T> void fill(

List<? super T> list, T obj);

public static <T> void copy(

List<? super T> dest,

List<? extends T> src);

...

}

How is reverse() implemented?


Capture2
Capture

public static void reverse(List<?> list) {

rev(list);

}

private static <T> void rev(List<T> list) {

for (int i = 0; i < 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);

}

}

}


Capture3
Capture

public static void reverse(List<?> list) {

rev(list);

}

private static <T> void rev(List<T> list) {

for (int i = 0; i < 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


Wildcard conclusions
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


ad