230 likes | 338 Views
ITEC 320. Lecture 10 Packages (2). Review. Packages What parts do they have? Syntax?. Store in a .ads file. Specification. Tells the compiler what the package does, but not how to do it. package PairPkg is type Pair is record x, y: Integer; end record
E N D
ITEC 320 Lecture 10 Packages (2)
Review • Packages • What parts do they have? • Syntax?
Store in a .ads file Specification • Tells the compiler what the package does, but not how to do it package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;
Body • Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;
Client with ada.integer_text_io; use ada.integer_text_io; with pairpkg; use pairpkg; procedure pairclient is p: Pair; begin p := newPair(1, 2); put(distanceToOrigin(p)); p.x:= 3; -- Syntax error end pairclient;
Code • Input parser • Reads a line • Returns int / String / Float based on whitespace • Specification • Body • Client What should be written first? Privacy?
Comparison • Java’s Scanner • Similarities • Differences
Currently What happens when you want to store Strings? packageIntStackPkgistypeStackTypeislimitedprivate;Stack_Is_Empty, Stack_Is_Full : Exception;functionIsEmpty (S : StackType) return Boolean;functionIsFull (S : StackType) return Boolean;procedure Push (I : Integer; S : inoutStackType);procedure Pop (S : inoutStackType);function Top (S : StackType) return Integer;privateMaxSize : Constant Integer := 1000;typeElementArrayisarray(1 .. MaxSize) of Integer;typeStackTypeisrecord Elements : ElementArray;TheTop : Integer := 0;end record;endIntStackPkg;
Generic • Formal parameters • Specification • Actual parameters • Client • Need: get away from specific types
Generic generic -- Generic parameters are declared here Size : Positive; -- Size of stack to createtypeItemTypeisprivate; -- Type of elements packageStackPkgistype Stack islimitedprivate;Stack_Empty: exception; Stack_Full : exception; functionisEmpty (s : Stack) return Boolean;functionisFull (s : Stack) return Boolean;procedure push (item : ItemType; s : inout Stack);procedure pop (s : inout Stack);function top (s : Stack) returnItemType;privatetypeStackElementsisarray(1 .. Size) ofItemType;type Stack isrecord Elements : StackElements; Top : Natural := 0;end record;endStackPkg;
Body with StackPkg; -- Make the generic stack package availableprocedureavgsis -- Create the two stack packages that are neededpackageintstkpkgisnewstackpkg(1000, integer);packagefltstkpkgisnewstackpkg(1000, float); -- package FltStkPkg is new StackPkg(size => 1000, ItemType=> Float);-- A use statement will simplify access to the package members use IntStkPkg; use FltStkPkg; Value : Integer; Count, Sum : Integer := 0;Running_Average : Float;-- Declare the two stack variablesIntStack : IntStkPkg.Stack; -- Must include package name -- here so that theFltStack : FltStkPkg.Stack; -- compiler can tell which stack -- is needed
Body(2) beginwhilenotend_of_fileloop get(Value); Sum := Sum + Value; Count := Count + 1;Running_Average := float(Sum) / Float(Count);IntStkPkg.push(Value, IntStack);FltStkPkg.push(Running_Average, FltStack);end loop;whilenotIsEmpty(IntStack) loop put(top(IntStack)); put(" "); put(top(FltStack)); new_line; pop(IntStack); pop(FltStack);end loop;endavgs; Note:
Procedures generic type ItemType is private; procedure generic_swap(left, right: in out ItemType); -- Implementation of generic procedure swap procedure generic_swap(left, right : in out ItemType) is old_left: constant ItemType := left; begin left := right; right := old_left; end swap; -- Client file that instantiates generic procedure swap procedure swap_int is new generic_swap(ItemType => Integer); i,j: Integer; begin swap_int(i,j);
Notes • Generic packages cannot be used directly • Make a derived type and use it • Syntax for delaying when a type is applied • Consider in the declare block for packages
Child packages • Additions to existing packages • Currently • Add to specification • Add to body • New • Add functions/procedures/types to existing package • What feature is this similar to?
Method • Come up with a name for the child package • New package is parent.child • ada.text_io… • Specification is named • parent-child.ads • Body is named • parent-child.adb
Rationale • Soft grouping • Really, it just adds name. to a package • Adds new capabilities using existing structure • Logical nesting • Helps guide thinking • Sharing of types between parent / children • Name clash avoidance • Stay away from Ada, System, or Interface
Implicit /Explicit • Consider • Which do you consider implicit, explicit? • What does implicit offer over explicit? • What does explicit offer over implicit? System.out.println(p1.distanceToOrigin()); versus put(distanceToOrigin(p1));
Example 2 // Java Pair p = new Pair(1, 2); Pair q = new Pair(3, 4); System.out.println(p.toString()); System.out.println (q.toString()); -- Ada declare Pair q = newPair(1, 2); Pair r = newPair(3, 4); begin put(toString(q)); put(toString(r));
Conflation • Joins together • Java • Data • Methods • Ada • Types • Function / Procedures Rationale for each choice?
Usage scenarios • What is Java best suited for? • What are Java’s weaknesses? • What is Ada best suited for? • What are Ada’s weaknesses?
Implementation • If you had to write a compiler • Would you choose Java or Ada?
Review • Packages