Ada Ada: Gen : Generi erics cs
Neil Mitchell
http://www.cs.york.ac.uk/~ndm/ads.pdf
Want to follow along?
Ada Ada: Gen : Generi erics cs Neil Mitchell Aga gain, aga - - PowerPoint PPT Presentation
Want to follow along? http://www.cs.york.ac.uk/~ndm/ads.pdf Ada Ada: Gen : Generi erics cs Neil Mitchell Aga gain, aga in, again, ag in, again ain Dave writes a linked list package for characters Sue writes a linked list package
Neil Mitchell
http://www.cs.york.ac.uk/~ndm/ads.pdf
Want to follow along?
Dave writes a linked list package for
characters
Sue writes a linked list package for integers Ed writes a linked list package for booleans What if Dave had written a linked list package
for <anything>?
Sue and Ed could have gone to the pub! Generics
Generics allows Dave to do this
Value Value
Function
Type Package
Generic Package
Value Action
Procedure
Type Function
Generic Function
procedur procedure e Swap_Float(X,Y: in out : in out Float) is is T : Float; begin begin T := X; X := Y; Y := T; end; end;
generic generic type type Item is private is private; procedur procedure Swap(X,Y: in out in out Item); procedur procedure e Swap(X,Y: in out in out Item) is is T: Item; begin begin T := X; X := Y; Y:= T; end end Swap;
procedur procedure e Swap_Float is new is new Swap(Float);
generic generic type type Element is private is private; package package List is is type type List is private is private; Nil : constant constant List; function function Null_Query(L : List) return return Boolean; function function Cons(Head : Element; Tail : List) return return List; ... -- other useful methods private private ... -- as before end end List;
Not Note: Save as “list.ads”
package body package body List is is function function Null_Query (L : List) return return Boolean is is begin return begin return L = Nil; end end Null_Query; function function Cons(Head : Element; Tail : List) return return List is is begin begin return new return new Cell'(Content => Head ,Next => Tail); end end Cons; end end List;
Not Note: Save as “list.adb”
with with List; -- import procedure procedure Test is is
package package List_Integer is new is new List(Integer);
Ns : List_Integer.List := List_Integer.Nil; begin begin Ns := List_Integer.Cons(Head => 6, Tail => Ns); ...; end end Test;
Not Note: Save as “test.adb”
generic generic type type Element is <somet is <something> hing>; package package List is is
limi
mited private ted private = use as parameter type,
declare variables
pri
priva vate te = l = limited pri imited priva vate te + assign and test
for equality
(<>) = pri
(<>) = priva vate te + treat as discrete type (T’First, T’Range, etc)
Requ
Request: est: print a list
procedure Put(L : List);
Impossible! An item in list is generic We don’t know how to write it to the screen Solu
Solution ion: the user tells us how
with procedure Element_Put(E : in Element);
generic generic type type Element is private is private; with procedure with procedure Element_Put(E : in Element); package package List is is procedure procedure Put(L : in in List); ... -- as before end end List; procedure procedure Put(L : in in List) is is begin begin if not if not Null_Query(L) then then Element_Put(Head(L));
with with Ada.Text_IO, List; procedure procedure Test is is package package List_Char is new is new List (Element => Character ,Element_Put => Ada.Text_IO.Put); Hi : List_Char.List := ...; begin begin List_Char.Put(Hi); end end Test;
Each List package provides an unlimited
number of values:
An alternative is to have one value in one
package
gener neric ic type type Element is is p priv ivate te; with proc rocedu edure re Put(E : in in Element); packa ckage ge One_List is is
procedure ure Reset; function
return Boolean; procedure ure Cons(Head : in in Element); ...
end d One_List;
with th List; packa ckage ge body body One_List is is pa pack ckag age List_Element is is n new ew List (Element => Element, Put => Put); The_List : List_Element.List; function
return Boolean is is begi egin ret n return urn List_Element.Null_Query(The_List); end end Null_Query; procedure ure Cons(Head : in Element) is is begin begin The_List := List_Element.Cons(Head, The_List); end end Cons; begin gin Reset; end d One_List;
with th One_List; pr proce cedur ure Classify is is package e Marks is new is new One_List(Integer, Num_Out); package e Age is new is new One_List(Natural, Num_Out); begin Marks.Cons(99); -- cheated, not caught Marks.Cons(70); -- revised hard Marks.Cons(-5); -- cheated, caught, expelled Marks.Put; Marks.Reset; -- get rid of last years marks if Age.Null_Query then Age.Cons(21);
Work through the exercises
If you are struggling
the open assessment