From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 31 Jan 93 08:42:18 GMT From: mab@wdl39.wdl.loral.com (Mark A Biggar) Subject: Re: Private Specifications Message-ID: <1993Jan31.084218.15890@wdl.loral.com> List-Id: In article <1993Jan30.034333.14737@seas.gwu.edu> willson@seas.gwu.edu (Stephen R. Willson) writes: >I am not sure I understand the purpose of placing a type specification in >the body of a package, as a way to "hide" the specification from the >"client". Does someone have an example of where someone would want to >hide the type in such a way? It allows for the complete hiding of the implementation of an Abstract Data Type. For example, lets use the usual example the stack data type: generic type ITEM is private; package Stacks is type Stack is Private; function CREATE return Stack; procedure Push(S: Stack; I: ITEM); function Pop(S: Stack); function IsEmpty(S: Stack); exception StackUnderflow; exception StackOverflow; private type StackImp; type Stack is access StackImp; end Stacks; Notice that nothing about the implementation of a Stack is visible outside the package even to the compiler. So now I can have alternative bodies as implementations: package body Stacks is -- linked list implemetation -- exception StackOverflow probably never happens type StackImp is record I: ITEM; Next: Stack; end record; ... end Stacks; or package body Stacks is -- bounded array implementation -- exception StackOverflow is very possible subtype StackIndex is Integer range 0..100; type StackBody is array 1..100 of ITEM; type Stack is record ToS: StackIndex := 0; Store: StackBody; end record; ... end Stacks; Switching to either of the above package bodies requires that I only recompile the body and relink my program. If the definition of the stack had been in the private part of the package spec, switching implementations would have required recompiling every unit the instantiated the package or depended on a such a unit. When a type is defined in the private part of a package, it is only logically hidden from the user, its representation still affects othe units that depend on the package, while something in the package body never has affect on anything outside the package. -- Mark Biggar mab@wdl1.wdl.loral.com