Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Wildcards in the Java™ Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus."— Presentation transcript:

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 List numbers = new ArrayList(); numbers.add(new Integer(7)); Number n = (Number)numbers.get(0); interface List extends Collection { void add(T element); T get(int index);... }

8 Java Genericity interface List extends Collection { void add(T element); T get(int index);... } List numbers = new ArrayList (); numbers.add(new Integer(7)); Number n = numbers.get(0);

9 Static typecheck List numbers =... numbers.add(”Seven”); Number n = numbers.get(0); List numbers =... numbers.add(”Seven”); Number n = (Number)numbers.get(0);

10 Static typecheck List numbers =... numbers.add(”Seven”); Number n = numbers.get(0); List numbers =... numbers.add(”Seven”); Number n = (Number)numbers.get(0); Runtime type error!

11 Static typecheck List numbers =... numbers.add(”Seven”); Number n = numbers.get(0); List numbers =... numbers.add(”Seven”); Number n = (Number)numbers.get(0); Compile time error

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

13 Object-Oriented Abstraction Common view on common properties Collection List Set List l =...; Collection c = l;

14 Object-Oriented Abstraction Pointwise subtyping Collection List Set List nl =...; Collection nc = nl;

15 Parametric Abstraction What is the common view? List ?

16 In the ”old” days......any List was just a 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 numbers =...; List things = numbers; things.add(”Seven”); Number n = (Number)number.get(0); Runtime type error!

18 Number[] numbers =...; Object[] things = numbers; things[0] = ”Seven”; Number n = numbers[0]; The Array approach Runtime store check on every assignment Object[] Number[] String[]

19 Number[] numbers =...; Object[] things = numbers; Things[0] = ”Seven”; Number n = numbers[0]; The Array approach Runtime store check on every assignment Object[] Number[] String[] Runtime type error!

20 Can we do the same for List? List List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); Runtime type error? ?

21 Can we do the same for List? Erasure: No type argument info at runtime List List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); Runtime type error? ?

22 Can we do the same for List? Erasure: No type argument info at runtime List List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); Runtime type error!

23 Can we do the same for List? No type argument info at runtime List List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); Compile time error!

24 The raw types approach List List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); ?

25 The raw types approach Compile time type check undermined List List 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 List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); Compile time warning

27 The raw types approach Compile time type check undermined List List numbers =...; List things = numbers; things.add(”Seven”); Number n = numbers.get(0); Compile time warning

28 The GJ approach Object List List numbers =...; Object things = numbers; things.add(”Seven”); Object o = things.get(0);

29 The GJ approach Object List List numbers =...; Object things = numbers; things.add(”Seven”); Object o = things.get(0); Compile time error!

30 The GJ approach Cannot use common interface Object List List numbers =...; Object things = numbers; things.add(”Seven”); Object o = things.get(0); Compile time error!

31 The GJ approach Cannot use common interface Object List List 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 ” List ?

34 Wildcards ”List of something ” List ?

35 Wildcards ”List of something ” List List numbers =...; List things = numbers; things.add(”Seven”); Object o = things.get(0);

36 Wildcards ”List of something ” List List numbers =...; List things = numbers; things.add(”Seven”); Object o = things.get(0);

37 Wildcards in Collections package java.util; public interface Collection { boolean containsAll(Collection c); boolean removeAll(Collection c);... }

38 Can we do better? Integer and Float are related List

39 Bounded Wildcards Numbers come out List List ints =...; List numbers = ints; Number n = numbers.get(0); numbers.add(.7F);

40 Bounded Wildcards Adding still prohibited List List ints =...; List numbers = ints; Number n = numbers.get(0); numbers.add(.7F);

41 Extends-bounds in Collections package java.util; public interface Collection { boolean containsAll(Collection c); boolean removeAll(Collection c); boolean addAll(Collection c);... }

42 What can we do about adding? Adding still prohibited List

43 Super-bounded Wildcards List List numbers =...; List ints = numbers; ints.add(7); Integer i = ints.get(0);

44 Super-bounded Wildcards Only Objects come out List List numbers =...; List ints = numbers; ints.add(7); Integer i = ints.get(0);

45 TreeSet Constructors package java.util; public class TreeSet implements OrderedSet { public TreeSet(OrderedSet c); public TreeSet(Collection c); public TreeSet(Comparator c);... }

46 Collections utility methods package java.util; public class Collections { public static void reverse(List list); public static void shuffle(List list); public static void fill( List list, T obj); public static void copy( List dest, List src);... } T

47 Subtyping with wildcards List

48 Subtyping with wildcards List

49 Type inference Better types to choose from T choose(T fst, T snd) {... } List numbers =... Set strings =... Collection c = choose(numbers,strings)

50 Type inference Built-in condition expression Boolean b =... List numbers =... Set strings =... Collection c = b ? numbers : strings

51 Capture package java.util; public class Collections { public static void reverse(List list); public static void shuffle(List list); public static void fill( List list, T obj); public static void copy( List dest, List src);... }

52 Capture package java.util; public class Collections { public static void reverse(List list); public static void shuffle(List list); public static void fill( List list, T obj); public static void copy( List dest, List src);... } How is reverse() implemented?

53 Capture public static void reverse(List list) { rev(list); } private static void rev(List 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); }

54 Capture public static void reverse(List list) { rev(list); } private static void rev(List 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

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


Download ppt "Wildcards in the Java™ Programming Language Neal Gafter with thanks to Mads Torgersen, University of Aarhus."

Similar presentations


Ads by Google