Generics - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Generics

Description:

Title: PowerPoint Presentation Last modified by: davise Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show Other titles – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 18
Provided by: csNyuEduf
Learn more at: https://cs.nyu.edu
Category:

less

Transcript and Presenter's Notes

Title: Generics


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 theres 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 ltgt) of
    Item
  • with function f(X Item) return Item
  • procedure
  • ArrayApplier(A,B in out ItemArray)
  • begin for I in 1 .. ALast loop
  • BI f(AI)
  • end loop
  • end

4
Instantiating an Ada generic
  • function incr(I Integer) return Integer is begin
    return I1 end
  • type IntArray is array(Integer rangeltgt) of
    Integer
  • function ApplyIncr is new ArrayApplier(Integer,Int
    Array,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 lttypename Tgt
  • int compare(const T v1, const T v2)
  • if (v1 lt v2) return -1
  • if (v2 lt 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 UnaryFunltT,Sgt
  • public S f(T x)
  • class IntSqrt implements UnaryFunltInteger,Floatgt
  • public Float f(Integer i)
  • return new Float(Math.sqrt(i))

8
Example continued
  • class ApplierltT,Sgt
  • public void ApplyToArray(
  • UnaryFunltT,Sgt Q, T A, S B)
  • for (int i0 i lt A.length i)
  • Bi Q.f(Ai)

9
Example continued
  • main
  • Integer A new Integer 1,2,3
  • Integer C new Integer3
  • IntSqrt ISQ new IntSqrt()
  • ApplierltInteger,Floatgt IFApp
  • new ApplierltInteger,Floatgt()
  • IFApp.ApplyToArray(ISQ,A,C)

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

11
Subtypes of generic types
  • LinkedListltIntegergt LI new LinkedListltIntegergt()
  • LinkedListltObjectgt 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
    LinkedListltObjectgt you cant pass an argument of
    type LinkedListltIntegergt

12
Wildcards
  • void printCollection(Collectionlt?gt c)
  • for (Object e c) System.out.println(e)
  • LinkedListltIntegergt L
  • printCollection(L)

13
Restricted type parameters and wildcards
  • ltT extends shapegt, ltT super shapegt
  • lt? extends shapegt, lt? super shapegt
  • void drawAll(Listlt? extends shapegt L)
  • for (shape S L) draw(S)
  • Type definition for max(c) (simplified)
  • public static
  • ltT extends Comparablelt? super Tgtgt
  • T max(CollectionltTgt coll)

14
Embedded Type Parametrization
  • LinkedListltLinkedListltIntegergtgt
  • static ListltListlt? extends Shapegtgt history
    new ArrayListltListlt? extends Shapegtgt
  • public void
  • drawAll(Listlt? extends Shapegt 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 LinkedListltIntegergt, 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.
Write a Comment
User Comments (0)
About PowerShow.com