comp.lang.ada
 help / color / mirror / Atom feed
* don't understand packages vs. objects/classes
@ 1994-11-27  4:42 Andrew E. Caldwell
  1994-11-27 14:58 ` Roger Labbe
  1994-11-28 15:02 ` Mark C. Chu-Carroll
  0 siblings, 2 replies; 5+ messages in thread
From: Andrew E. Caldwell @ 1994-11-27  4:42 UTC (permalink / raw)


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.

-andy		caldwell@seas.ucla.edu




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: don't understand packages vs. objects/classes
  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
  1 sibling, 1 reply; 5+ messages in thread
From: Roger Labbe @ 1994-11-27 14:58 UTC (permalink / raw)


As declared, you cannot have two or more stacks. The key is to define
a data type called stack and encapsulate it in a package (which I'll
call STACKS in a failure of creativity). THus you might have:

-- Lame implementation without error handling or other necessary stuff.
generic
   type element is private;
package STACKS is

   type stack is private;

   procedure push (s     : stack;
                   value : element);

   ... rest of spec
end STACKS;

Then in your procedure you can declare
 
package int_stack is new stacks (element => integer);
package float_stack is new stacks (element => your_float);

   x : int_stack;
   y, z : float_stack;
begin
   push(x, 3);


Now whether you want to consider this library based or object based
is up to you. But how different is the C++ equivalent

   stack<int> x;

   x.push(3);

The x is outside the push instead of a parameter as in Ada, but I consider
the code equivalent (as far as this example goes).

As far as class hierarchies go Ada 83 doesn't really have them. You can use
derived types to inherit operations and add operations, but you cannot
add new components. Briefly, Ada 94 allows tagged types which may be
extended by derivation to add new components. This is a topic that is
too complex for a single post, and probably not to important to someone
just learning the language. But yes, packages in Ada 83 are limited; they
are better at implementing encapsulation than object oriented features.

Get the Ada 9x rationale from the Ada web server if you want to read
a good introduction to Ada 94 and object oriented programming. I don't
have the address right here, but you can find it easily in the newsgroup;
alternatively, send me email and I'll send it to you.

Roger Labbe




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: don't understand packages vs. objects/classes
  1994-11-27 14:58 ` Roger Labbe
@ 1994-11-28 11:35   ` David Weller
  0 siblings, 0 replies; 5+ messages in thread
From: David Weller @ 1994-11-28 11:35 UTC (permalink / raw)


In article <3ba6qu$6rs@mandolin.gcr.com>, Roger Labbe <dodger@gcr.com> wrote:
>generic
>   type element is private;
>package STACKS is
>
>   type stack is private;
>
>   procedure push (s     : stack;
*ahem*			   ^insert "in out" here :-)
>                   value : element);
>
>Get the Ada 9x rationale from the Ada web server if you want to read
>a good introduction to Ada 94 and object oriented programming. I don't
>have the address right here, but you can find it easily in the newsgroup;
>alternatively, send me email and I'll send it to you.
>
Hmm, I haven't checked the Ada Homepage lately, but I suppose the
Rationale is indeed there.  In any case, the WWW homepage is:
	http://lglwww.epfl.ch/Ada/   (don't forget the last /)
Also, you can ftp the Rationale from:
	ajpo.sei.cmu.edu, look under public/ada9x/rm9x/v5.0
(I think there's a v5.7 dir, but I don't remember if the Rationale is
there)


-- 
Proud (and vocal) member of Team Ada! (and Team OS/2)        ||This is not your
   	      Ada -- Very Cool.  Doesn't Suck.               ||  father's Ada 
For all sorts of interesting Ada tidbits, run the command:   ||________________
"finger dweller@starbase.neosoft.com | more" (or e-mail with "finger" as subj.)
	|"Quitting C++ isn't so difficult, provided you show as much |
	|	persistence stopping as you did starting." dweller   |



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: don't understand packages vs. objects/classes
  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 15:02 ` Mark C. Chu-Carroll
  1 sibling, 0 replies; 5+ messages in thread
From: Mark C. Chu-Carroll @ 1994-11-28 15:02 UTC (permalink / raw)


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>

-- 



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: don't understand packages vs. objects/classes
@ 1994-11-29 17:04 Bennett, Chip (KTR) ~U
  0 siblings, 0 replies; 5+ messages in thread
From: Bennett, Chip (KTR) ~U @ 1994-11-29 17:04 UTC (permalink / raw)


"Andrew E. Caldwell" <caldwell@TYPHOON.SEAS.UCLA.EDU> wrote:

> 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.

I'm having a hard time understanding why we are compelled to keep giving you
generics as the solution to your question.  It's sort of like using an
elephant gun to kill an ant.  It seemed to me that your question had more to
do with how to get many stacks, not how to get stacks of different types.

If you're only implementing stacks of integers, you need only declare a
abstract stack type that is instantiated a number of times (for each stack),
and managed by the push and pop routines in the same package.  A possible
spec would be:

     package IntStacks is

        type Stack is private;

        procedure Push (S : in out Stack; Item : in Integer);

        function Pop (S : in out Stack) return Integer;

     private

        type Stack is ...  -- actual representation
                           -- i.e. array of integers,
                           --      linked list of integers, etc.

     end Stacks;

You might use this stack type in the following way:

     with IntStacks; use IntStacks;
     procedure YourProg is
        Stack1 : Stack;
        Stack2 : Stack;
     begin
        Push (Stack1, 34);
        Push (Stack2, 12);
     end YourProg;

Generics is a beautiful concept, way ahead of its time in 1983, but we
sometimes have a tendency to use them in every answer.  They give a great
deal of flexibility, but are not needed in every solution.

*****************************************************************
* Chip Bennett, GDE Systems Inc | BennettC@j64.stratcom.af.mil  *
* USSTRATCOM/J64213             | Voice (402)294-7360           *
* 901 SAC Blvd, Suite 2B24      | FAX   (402)294-7912           *
* Offutt AFB, NE 68113-6600     | Proud member of Team Ada      *
* Opinions expressed here are my own _so_,  TTFWTW              *
*****************************************************************



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1994-11-29 17:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
1994-11-29 17:04 Bennett, Chip (KTR) ~U

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