Presentation is loading. Please wait.

Presentation is loading. Please wait.

Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want.

Similar presentations


Presentation on theme: "Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want."— Presentation transcript:

1 Generics

2 Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want to write a single array applier for any function from type T1 to T2? Dynamic method binding allows the type of “self” to be variable, but there’s no way to get 2 variable types.

3 Generics in Ada Essentially a structured macro. generic type Item is private; type ItemArray is array (Integer range <>) of Item; with function f(X: Item) return Item; procedure ArrayApplier(A,B: in out ItemArray) begin for I in 1.. A’Last loop B[I] = f(A[I]); end loop end

4 Instantiating an Ada generic function incr(I: Integer) return Integer is begin return I+1; end; type IntArray is array(Integer range<>) of Integer; function ApplyIncr is new ArrayApplier(Integer,IntArray,incr);

5 Implementation Each instantiation of the generic is built by a preprocessor at compile time, and compiles to a separate routine. Like a macro.

6 C++ templates template int compare(const T &v1, const T &v2) { if (v1 < v2) return -1; if (v2 < v1) return 1; return 0; } int I,J; compare(I,J); // int compare double X,Y; compare(X,Y); // double compare compare(I,X); // compiler error

7 Java generics: example interface UnaryFun { public S f(T x); } class IntSqrt implements UnaryFun { public Float f(Integer i) { return new Float(Math.sqrt(i)); }}

8 Example continued class Applier { public void ApplyToArray( UnaryFun Q, T[] A, S[] B) { for (int i=0; i < A.length; i++) B[i] = Q.f(A[i]); }}

9 Example continued … main … Integer A[] = new Integer[] {1,2,3}; Integer C[] = new Integer[3]; IntSqrt ISQ = new IntSqrt(); Applier IFApp = new Applier (); IFApp.ApplyToArray(ISQ,A,C);

10 Generic types LinkedList list = new LinkedList(); list.add(new Integer(1)); Integer num = (Integer) list.get(0); LinkedList L = new LinkedList (); L.add(new Integer(1)); Integer num = list.get(0);

11 Subtypes of generic types LinkedList LI = new LinkedList (); LinkedList LO = LI; // Type error! LO.add(new String(“Hello”)); Difference between extensional and abstraction view of types. Therefore if a method has a parameter of type LinkedList you can’t pass an argument of type LinkedList

12 Wildcards void printCollection(Collection c) { for (Object e : c) { System.out.println(e);} LinkedList L; printCollection(L);

13 Restricted type parameters and wildcards, void drawAll(List L) { for (shape S : L) draw(S); } Type definition for max(c) (simplified): public static > T max(Collection coll)

14 Embedded Type Parametrization LinkedList > static List > history = new ArrayList >; public void drawAll(List shapes) { history.addLast(shapes); for (Shape s: shapes) s.draw(this); }

15 Implementation: Type erasure No record of the parameter value at run time. If L is of type LinkedList, all the interpreter knows at runtime is that it is of type LinkedList. One subroutine for ApplyToArray, not a separate routine for each different argument type (as in Ada and C++). Possible because parameters are reference types and all the same size.

16 Bug, not a feature Done for compatibility with legacy code. In complex cases, leads to need for casts, runtime type checking, type unsafe code.

17 Morals Backward compatibility is the source of much grief. (Forward compatibility is a myth.) It is challenging to develop a type theory of complex entities (collections, functions, etc.) that is both adequate and well-defined.


Download ppt "Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want."

Similar presentations


Ads by Google