slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Genericity Parameterizing by Type PowerPoint Presentation
Download Presentation
Genericity Parameterizing by Type

Loading in 2 Seconds...

play fullscreen
1 / 16

Genericity Parameterizing by Type - PowerPoint PPT Presentation


  • 73 Views
  • Uploaded on

Genericity Parameterizing by Type. Generic Class. One that is parameterized by type Works when feature semantics is common to a set of types On object declaration the parameter is assigned a type For example catalogue : ARRAY [ PERSON ] We want an array of references to persons

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 'Genericity Parameterizing by Type' - sachi


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
slide1

Genericity

Parameterizing by Type

generic class
Generic Class
  • One that is parameterized by type
    • Works when feature semantics is common to a set of types
  • On object declaration the parameter is assigned a type
    • For example

catalogue : ARRAY [ PERSON ]

    • We want an array of references to persons
    • All the array operations for catalogue are customized to use persons
common generic classes
Common Generic Classes
  • Collection classes – classes that are collections of objects
    • Strong typing requires specifying a type
    • But feature semantics is independent of type
  • Examples
    • Sets, Stacks, Arrays, Queues, Sequences

a : ARRAY [ MATRIX_ELEMENT ]

a : ARRAY [ INTEGER ]

a : ARRAY [ STACK [ ELEPHANTS ] ]

your generic classes
Your Generic Classes
  • You can write generic classes
  • Why is this useful?
    • reuse
    • reliability
understanding genericity
Understanding Genericity
  • We need
    • lists of INTEGER, lists of BOOK, lists of STRING etc.
  • Without genericity we have
    • INTEGER_LIST, BOOK_LIST, STRING_LIST
  • Problem — Reusability.
    • The basic operations (e.g. extend)are essentially the same.
    • Have to re-write essentially the same code over and over again.
    • Violation of the Single Choice Principle — changing STACK requires multiple changes, instead of a change at a single point.
generic stack
Generic Stack

class STACK [ G ] feature

count : INTEGER -- number of elements

empty : BOOLEAN is do ... end

full : BOOLEAN is do ... end

item : Gis do ... end

put ( x : G ) is do ... end

remove is do ... end

end -- STACK

  • Can use parameter G wherever a type is expected
generic array
Generic Array

class ARRAY [ P ] creation makefeature

make ( minIndex , maxIndex : INTEGER ) is do ... end

lower, upper, count : INTEGER

put ( value : P ; index : INTEGER ) is do ... end

infix "@" , item ( index : INTEGER ) : P is do ... end

end -- ARRAY

using the generic array
Using the Generic Array

circus : ARRAY [ STACK [ ELEPHANTS ] ]

create circus.make ( 1 , 200 )

st_el : STACK [ ELEPHANTS ] -- element to put in the array

create st_el

circus.put ( st_el , 30 ) -- put an element into the array

st_el2 : STACK [ ELEPHANTS ]

st_el2 := circus @ 101 -- get an element from the array

types of genericity
Types of Genericity
  • Types
    • Unconstrained
    • Constrained
  • The previous examples showed unconstrained genericity
    • Any type could be passed as a parameter
constrained genericity
Constrained Genericity
  • Used when the generic type parameters must satisfy some conditions
  • The following makes sense only if P has thefeature ≥

class VECTOR [ P ] feature

...

minimum ( x , y : P ) : P is do

if x ≥ y then

Result := y

else

Result := x

end

...

end

How we enforce

constraints is

discussed in

Inheritance Techniques

discussion on genericity
Discussion on Genericity
  • What programming languages offer genericity that you know of? Java? C++? Other?
  • C++ has the template: Set < int > s ;
  • Java has no genericity – can it be faked?
  • What is the effect of genericity on
    • compile time
    • size of the generated code
    • execution time
    • execution space
  • Warning: generics cheap in Eiffel – expensive in C++
java stack
Java Stack

publicclass Stack {

private int count;

private int capacity;

private int capacityIncrement;

privateObject[] itemArray; // instead of ARRAY[G]

Stack() { //constructor limited to class name

count= 0;

capacity= 10;

capacityIncrement= 5;

itemArray= new Object[capacity];

// no pre/postconditions/invariants

}

public boolean empty() {

return (count == 0);

}

java stack cont
Java Stack (cont.)

publicvoidpush(Object x) {

if(count == capacity) {

capacity += capacityIncrement;

Object[] tempArray= new Object[capacity];

for(int i=0; i < count; i++)

tempArray[i]= itemArray[i];

itemArray= tempArray;

}

itemArray[count++]= x;

}

publicObjectpop() {

if(count == 0) // no preconditions;

// hence defensive programming

return null;

else return itemArray[--count];

}

java stack cont1
Java Stack (cont.)

publicObjectpeek() {

if(count == 0)

return null;

else return itemArray[count-1];

}

} // end Stack; no class invariant

publicclass Point {

public int x;

public int y;

}

java stack runtime error
Java Stack runtime error

class testStack {

publicstaticvoid main(String[] args) {

Stack s = new Stack();

Point upperRight = new Point();

upperRight.x= 1280; // breaks information hiding

upperRight.y= 1024; // cannot guarantee any contract

s.push("red");

s.push("green"); // push some String

s.push("blue"); // objects on the stack

s.push(upperRight); // push a POINT object on the stack

while(!s.empty()) { // cast all items from OBJECT

String color = (String) s.pop(); // to STRING in order to print

System.out.println(color);

}}} // causes a run-time error in Java

// ClassCastException: Point at testStack.main(testStack.java:18)

// In Eiffel it is a compile time error

does run time vs compile time matter
Does run-time vs. compile time matter?
  • Principle: When flying a plane, run-time is too late to find out that you don’t have landing gear!
  • Always better to catch errors at compile time!
  • This is the main purpose of Strong Typing [OOSC2, Chapter 17].
  • Genericity helps to enforce Strong Typing, i.e. no run-time typing errors
    • LIST[INTEGER]
    • LIST[BOOK]
    • LIST[STRING]