From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,155b49c9f46f66ec X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-11-28 07:11:25 PST Path: nntp.gmd.de!xlink.net!howland.reston.ans.net!gatech!udel!stimpy.eecis.udel.edu!carroll From: carroll@stimpy.eecis.udel.edu (Mark C. Chu-Carroll) Newsgroups: comp.lang.ada Subject: Re: don't understand packages vs. objects/classes Date: 28 Nov 1994 15:02:48 GMT Organization: University of Delaware, Newark Message-ID: <3bcreo$6qj@louie.udel.edu> References: NNTP-Posting-Host: stimpy1.eecis.udel.edu Date: 1994-11-28T15:02:48+00:00 List-Id: In article 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. --