ada ada gen generi erics cs
play

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


  1. Want to follow along? http://www.cs.york.ac.uk/~ndm/ads.pdf Ada Ada: Gen : Generi erics cs Neil Mitchell

  2. Aga gain, aga in, again, ag in, again ain  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

  3. Gen Generic? eric? Function Value Value Procedure Value Action Generic Function Type Function Generic Package Type Package

  4. Swap_ Sw ap_Flo loat at procedure procedur e Swap_Float(X,Y: in out : in out Float) is is T : Float; begin begin T := X; X := Y; Y := T; end; end;

  5. Sw Swap ( ap (gener generic ic proc proced edure) ure) generic generic type Item is private type 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 T := X; X := Y; Y:= T; end begin end Swap; -- an instantiation, which we use procedure procedur e Swap_Float is new is new Swap(Float);

  6. Gen Generic p eric pack ackage sp age spec ecific ificat ation ion generic generic type type Element is private is private; package List is package is type type List is private is private; Nil : constant constant List; function Null_Query(L : List) return function return Boolean; function function Cons(Head : Element; Tail : List) return List; return ... -- other useful methods private private ... -- as before Not Note: end List; end Save as “ list.ads ”

  7. Generic p Gen eric pack ackage b age bod ody package body List is package body is function Null_Query (L : List) return function return Boolean is is begin return L = Nil; begin return end end Null_Query; function Cons(Head : Element; Tail : List) function return return List is is begin begin return new return new Cell'(Content => Head ,Next => Tail); end Cons; end Not Note: end end List; Save as “ list.adb ”

  8. Using Us ing a ge a gener neric pac ic packa kage ge with List; -- import with procedure procedure Test is is -- instantiate package package List_Integer is new is new List(Integer); -- use Ns : List_Integer.List := List_Integer.Nil; begin begin Ns := List_Integer.Cons(Head => 6, Tail => Ns); ...; Not Note: end Test; end Save as “ test.adb ”

  9. Gen Generic typ eric type pa e paramet rameters ers generic generic type Element is <somet type is <something> hing>; package List is package is ted private = use as parameter type,  limi mited private declare variables te + assign and test  pri priva vate te = l = limited pri imited priva vate for equality  (<>) = pri (<>) = priva vate te + treat as discrete type ( T’First , T’Range , etc)

  10. Proced Pro cedures as ures as Pa Param rameters eters  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);

  11. Implement Im lementing ing Put Put generic generic type type Element is private is private; with procedure Element_Put(E : in Element); with procedure package package List is is procedure procedure Put(L : in in List); ... -- as before end List; end procedure procedure Put(L : in in List) is is begin begin if not if not Null_Query(L) then then Element_Put(Head(L));

  12. Using Us ing Put Put with Ada.Text_IO, List; with procedure procedure Test is is package List_Char is new package is new List (Element => Character ,Element_Put => Ada.Text_IO.Put); Hi : List_Char.List := ...; begin begin List_Char.Put(Hi); end end Test;

  13. One p ne pack ackage, on ge, one e val value? ue?  Each List package provides an unlimited number of values: ◦ Every operation takes a List parameter ◦ Which data structure to operate on ◦ Abstract Data Type (ADT)  An alternative is to have one value in one package ◦ No more saying which data structure ◦ Can sometimes be simpler ◦ Abstract Object

  14. Abst bstract ract Ob Object ject: Spe : Specific ificat ation ion 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 -- Note: no exported type or constants! -- All the state is in the body -- A new facility (replaces constant Nil) procedure ure Reset; function on Null_Query return return Boolean; procedure ure Cons(Head : in in Element); ... -- Note: no private section! end d One_List;

  15. Abst bstract ract Ob Object ject: Bod : Body with th List; packa ckage ge body body One_List is is pack pa ckag age List_Element is is n new ew List (Element => Element, Put => Put); The_List : List_Element.List; function on Null_Query return 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;

  16. Abst bstract ract Ob Object ject: Use : Use 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);

  17. What t hat to o do do no now? w?  Work through the exercises ◦ No more of us talking at the start of practicals ◦ Go from where you are ◦ Get as far as you can  If you are struggling ◦ Stick your hand up, get some help now ◦ You will be expected to be able to program Ada for the open assessment ◦ You will not get Ada help during that time

More recommend