comp.lang.ada
 help / color / mirror / Atom feed
From: mab@wdl39.wdl.loral.com (Mark A Biggar)
Subject: Re: Private Specifications
Date: 31 Jan 93 08:42:18 GMT	[thread overview]
Message-ID: <1993Jan31.084218.15890@wdl.loral.com> (raw)

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

             reply	other threads:[~1993-01-31  8:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-01-31  8:42 Mark A Biggar [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-01-31  7:41 Private Specifications Bob Kitzberger
1993-01-30  3:43 Stephen R. Willson
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox