$B.1 GENERICITY 1169 Static typing may be considered too restrictive here:the only real requirement is that the two actual arguments passed to any call of swap should be of the same type,and that their type should also be applied to the declaration of the local variable t.It does not matter what this type actually is as long as it satisfies these properties. In addition the arguments must be passed in in out mode,so that the procedure can change their values.This is permitted in Ada. Genericity provides a tradeoff between too much freedom,as with untyped languages,and too much restraint,as with Pascal.In a generic language you may declare G as a generic parameter of swap or an enclosing unit.Ada indeed offers generic routines, along with the generic packages described in chapter 33.In quasi-Ada you can write: generic type G is private, procedure swap (x,y:in out G)is :G, begin t:=x.x:=yy:=t; end swap; The only difference with real Ada is that you would have to separate interface from implementation,as explained in the chapter on Ada.Since information hiding is irrelevant for the discussion in this chapter,interfaces and implementations will be merged for ease of presentation. The generic...clause introduces type parameters.By specifying G as "private",the writer of this procedure allows himself to apply to entities oftype G(x,y and 1)operations available on all types,such as assignment or comparison,and these only. The above declaration does not quite introduce a routine but rather a routine pattern; to get a directly usable routine you will provide actual type parameters,as in procedure int swap is new swap (INTEGER). procedure str_swap is new swap (STRING); etc.Now assuming that i and j are variables of type INTEGER,s and t of type STRING, then of the following calls int swap (i,j),str swap (s,t);int swap (i,s),str swap (s,j),str swap (i,j), all but the first two are invalid,and will be rejected by the compiler. More interesting than parameterized routines are parameterized packages.As a minor variation of our usual stack example,consider a queue package,where the operations on a queue (first-in,first out)are:add an element;remove the oldest element added and not yet removed;get its value;test for empty queue.The interface is:§B.1 GENERICITY 1169 Static typing may be considered too restrictive here: the only real requirement is that the two actual arguments passed to any call of swap should be of the same type, and that their type should also be applied to the declaration of the local variable t. It does not matter what this type actually is as long as it satisfies these properties. In addition the arguments must be passed in in out mode, so that the procedure can change their values. This is permitted in Ada. Genericity provides a tradeoff between too much freedom, as with untyped languages, and too much restraint, as with Pascal. In a generic language you may declare G as a generic parameter of swap or an enclosing unit. Ada indeed offers generic routines, along with the generic packages described in chapter 33. In quasi-Ada you can write: generic type G is private; procedure swap (x, y: in out G) is t: G; begin t := x; x := y; y := t; end swap; The only difference with real Ada is that you would have to separate interface from implementation, as explained in the chapter on Ada. Since information hiding is irrelevant for the discussion in this chapter, interfaces and implementations will be merged for ease of presentation. The generic… clause introduces type parameters. By specifying G as “private”, the writer of this procedure allows himself to apply to entities of type G (x, y and t) operations available on all types, such as assignment or comparison, and these only. The above declaration does not quite introduce a routine but rather a routine pattern; to get a directly usable routine you will provide actual type parameters, as in procedure int_swap is new swap (INTEGER); procedure str_swap is new swap (STRING); etc. Now assuming that i and j are variables of type INTEGER, s and t of type STRING, then of the following calls int_swap (i, j); str_swap (s, t); int_swap (i, s); str_swap (s, j); str_swap (i, j); all but the first two are invalid, and will be rejected by the compiler. More interesting than parameterized routines are parameterized packages. As a minor variation of our usual stack example, consider a queue package, where the operations on a queue (first-in, first out) are: add an element; remove the oldest element added and not yet removed; get its value; test for empty queue. The interface is: