comp.lang.ada
 help / color / mirror / Atom feed
From: carroll@stimpy.eecis.udel.edu (Mark C. Chu-Carroll)
Subject: Re: don't understand packages vs. objects/classes
Date: 28 Nov 1994 15:02:48 GMT
Date: 1994-11-28T15:02:48+00:00	[thread overview]
Message-ID: <3bcreo$6qj@louie.udel.edu> (raw)
In-Reply-To: Czwt26.G0B@seas.ucla.edu

In article <Czwt26.G0B@seas.ucla.edu> caldwell@typhoon.seas.ucla.edu (Andrew E. Caldwell) writes:
>Could someone please help me understand this.  Either there is a large
>group of syntactic constructs I missed in the manual, or I am
>still stuck in c++/smalltalk mode, or I just don't get it.
>
>It seems to me (in my VERY limited Ada knowledge) that packages are 
>like classes for OOP purposes.  They have the features of data hiding, and
>binding procedures with data, hiding implementation, etc. But, how
>do you get more than one of them in a procedure/function.
>
>For instance, if I define the package STACK to have the procedures
>POP which returns say, an integer, and PUSH(X:integer), and I want
>to use my STACK in a procedure, I do something like (please, although my
>Ada style may not be the usual as I'm new, I think it's clear what I'm asking,
>so...)
>
>with STACK;
>procedure foo is
>use stack;
>test:integer;
>
>begin
>--a procedure body
>end foo;
>
>and inside foo I can call test = POP;
>and this works.  But, what if foo needed TWO stacks, or three? As STACK
>doesn't appear to be named, it doesn't look like an object.  It's a 
>support library, rather than a data type?  But, that wasn't what I 
>thought it was supposed to be.  If it is just a library of functions,
>then does Ada have a class/object structre?  How about all the OOP
>subjects I've  seen on this list lately- surely it must.

The problem that you're having is that you've assumed that two
seperate concepts must be combined.

In C++ (and friends), the idea of class (data type with a set of
dynamically bound operations), and module (block of encapsulated code
with limited external access) are combined into a single feature; a
class both defines the type and the visibility.

In Ada (and many other languages like Modula-2/3, Dylan, etc), these
two things are seperated.

In Ada:

-  a package is a *module*, not a type or an object. It contains
 a collection of procedures and types, and exports a limited subset of
 the entities declared inside. 

- a class is a *type*. 

To implement the stack you mentioned above, you'd define a package
STACK, which exported a stack type, and a collection of procedures.
Roughly (pardoning syntax errors; I haven't had the opportunity to
write much Ada code):

generic 
   type Item is private;
package STACK is
   type T is private;
   procedure MakeStack(size : in Integer) return T;
   procedure Push(s : in, out T, i : in item);
   procedure Pop(s : in, out T) return Item;
private
   type T is record
      ...
   end;
...
end Stack;

And the procedure you use would be:

package INT_STACK is new STACK(int);
with  INT_STACK;
procedure foo is
   st : INT_STACK.T;
   test:integer;
begin
--a procedure body
end foo;

Within that procedure, calls would look like: "Push(st,test)"...  And
you could define multiple stacks by declaring multiple variables of
type INT_STACK.T.


	<MC>

-- 



  parent reply	other threads:[~1994-11-28 15:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-11-27  4:42 don't understand packages vs. objects/classes Andrew E. Caldwell
1994-11-27 14:58 ` Roger Labbe
1994-11-28 11:35   ` David Weller
1994-11-28 15:02 ` Mark C. Chu-Carroll [this message]
  -- strict thread matches above, loose matches on Subject: below --
1994-11-29 17:04 Bennett, Chip (KTR) ~U
replies disabled

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