comp.lang.ada
 help / color / mirror / Atom feed
* about abstract data types and pointer
@ 1996-09-21  0:00 Benoit Rochefort
  1996-09-24  0:00 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Benoit Rochefort @ 1996-09-21  0:00 UTC (permalink / raw)



Reposting article removed by rogue canceller.

Hi! I'm new with ADA but i'm a programmer since a long time.

I use the ADA83 standard (but with the gnat compiler than can compile
ADA95).

I have some questions to start with:

1- Is there a way to define a procedure type like in MODULA-2 in
   a way such:

     TYPE PROC IS PROCEDURE(IN E:ELEMENT);

   Or like in C where we can define a pointer to such a fonction?
   (how can i define an access type in ADA that "points" to such
   a procedure?)

   This would be very helpful in the case of a list type (for example),
   having an operation defined like that:

     PROCEDURE Map(L: IN List; P: PROC);

   Which map the P Procedure to each element of the list. P would
   obviously cause a side effect (like printing an element).

   OR can I define procedure Map something like that:

     PROCEDURE Map(L: IN List; P: PROCEDURE(IN ELEMENT));

   Please tell me how i can call the p procedure passed as a parameter
   to Map in the code; maybe something like:

     P(E);

   Please don't suggest me to use a generic proc, because it's NOT
   what i want; Map could be called for MANY procedures, not just one
   for each instance of a List. example:

     Map(L, Put);    -- Print each element on stdout.
     Map(L, CountE); -- Count the number of elements (side effect).
     Map(L, Draw);   -- Draw the value of L on a graph.

   Of course; Draw and Put have the side effect of "writing".

   Count could be written like this:

   FUNCTION Count(L: IN List) RETURN Natural IS
     Nb : Natural := 0;
     PROCEDURE CountE IS
     BEGIN
       Nb := Nb + 1;
     END CountE;
   BEGIN
     Map(L, CountE);
     RETURN Nb;
   END Count;

2- Is there a way to totally hide the representation of an abstract
   data type in the specification package? I really don't understand
   why we should specify the representation of such a type in the
   PRIVATE part of the specification package (except for compiler
   needs -- that is simpler for the compiler :-) ). Even in C we
   can hide the representation of an abstract data type from the 
   header file. Why not in ADA which is supposed to be better for 
   the information hiding principle?

3- Is there a way to "add" or change attributes to a new type that i 
   have elaborated as an abstract data type (in a package).

   Ex: L'First could have another meaning here if L is a List.
       List'Nb could denote the number of elements of a List L.

   I know i can (and sure i must) code these as functions but i just
   want to use them as attribute; which could be much more natural here.

   Is that what RENAMES is for?

4- Is there a way to overload the ":=" operator if List is a
   limited private type?

   ex:

       L1 := L2;

   could make a copy of all elements of L1 into L2. This is much more
   natural than

       Copy(L1, L2);

5- Do the ADA83 standard specify that the deallocation of memory is
   automatically done? Or must I ensure that my program does it?

   ex:
   
   PROCEDURE Test IS
     L: List;
   BEGIN
     Add(L, 3);
     Add(L, 4);
   END Test;

   If L is a pointer to some NEW element created; will it be freed
   automaticaly on return of the PROCEDURE Test? Or must i provide
   an operation to free memory (like Dispose)?

   The next problem is that probably i would have allocated some extra
   memory for the elements of L; will it be also freed automaticaly?
   Some language do it (SML, SMALLTALK), some don't (MODULA-2, C, C++)

   However, we could define a destructor in C++; is it possible in ADA
   that the destructor would be automatticaly called when the
   object fall out of scope?

6- Now the easy one.
   Is there a function or procedure to read a character on the
   keyboard without the need to press enter WHICH IS DEFINED ON THE
   STANDARD (independant of the used compiler and machine).


I know that's a lot of questions to answer but that would be VERY
appreciated.

Please reply by mail. Thank you.

-- 

Benoit Rochefort
jk791840@er.uqam.ca

-- 

Benoit Rochefort
jk791840@er.uqam.ca





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

* about abstract data types and pointer
@ 1996-09-21  0:00 Benoit Rochefort
  0 siblings, 0 replies; 5+ messages in thread
From: Benoit Rochefort @ 1996-09-21  0:00 UTC (permalink / raw)



Hi! I'm new with ADA but i'm a programmer since a long time.

I use the ADA83 standard (but with the gnat compiler than can compile
ADA95).

I have some questions to start with:

1- Is there a way to define a procedure type like in MODULA-2 in
   a way such:

     TYPE PROC IS PROCEDURE(IN E:ELEMENT);

   Or like in C where we can define a pointer to such a fonction?
   (how can i define an access type in ADA that "points" to such
   a procedure?)

   This would be very helpful in the case of a list type (for example),
   having an operation defined like that:

     PROCEDURE Map(L: IN List; P: PROC);

   Which map the P Procedure to each element of the list. P would
   obviously cause a side effect (like printing an element).

   OR can I define procedure Map something like that:

     PROCEDURE Map(L: IN List; P: PROCEDURE(IN ELEMENT));

   Please tell me how i can call the p procedure passed as a parameter
   to Map in the code; maybe something like:

     P(E);

   Please don't suggest me to use a generic proc, because it's NOT
   what i want; Map could be called for MANY procedures, not just one
   for each instance of a List. example:

     Map(L, Put);    -- Print each element on stdout.
     Map(L, CountE); -- Count the number of elements (side effect).
     Map(L, Draw);   -- Draw the value of L on a graph.

   Of course; Draw and Put have the side effect of "writing".

   Count could be written like this:

   FUNCTION Count(L: IN List) RETURN Natural IS
     Nb : Natural := 0;
     PROCEDURE CountE IS
     BEGIN
       Nb := Nb + 1;
     END CountE;
   BEGIN
     Map(L, CountE);
     RETURN Nb;
   END Count;

2- Is there a way to totally hide the representation of an abstract
   data type in the specification package? I really don't understand
   why we should specify the representation of such a type in the
   PRIVATE part of the specification package (except for compiler
   needs -- that is simpler for the compiler :-) ). Even in C we
   can hide the representation of an abstract data type from the 
   header file. Why not in ADA which is supposed to be better for 
   the information hiding principle?

3- Is there a way to "add" or change attributes to a new type that i 
   have elaborated as an abstract data type (in a package).

   Ex: L'First could have another meaning here if L is a List.
       List'Nb could denote the number of elements of a List L.

   I know i can (and sure i must) code these as functions but i just
   want to use them as attribute; which could be much more natural here.

   Is that what RENAMES is for?

4- Is there a way to overload the ":=" operator if List is a
   limited private type?

   ex:

       L1 := L2;

   could make a copy of all elements of L1 into L2. This is much more
   natural than

       Copy(L1, L2);

5- Do the ADA83 standard specify that the deallocation of memory is
   automatically done? Or must I ensure that my program does it?

   ex:
   
   PROCEDURE Test IS
     L: List;
   BEGIN
     Add(L, 3);
     Add(L, 4);
   END Test;

   If L is a pointer to some NEW element created; will it be freed
   automaticaly on return of the PROCEDURE Test? Or must i provide
   an operation to free memory (like Dispose)?

   The next problem is that probably i would have allocated some extra
   memory for the elements of L; will it be also freed automaticaly?
   Some language do it (SML, SMALLTALK), some don't (MODULA-2, C, C++)

   However, we could define a destructor in C++; is it possible in ADA
   that the destructor would be automatticaly called when the
   object fall out of scope?

6- Now the easy one.
   Is there a function or procedure to read a character on the
   keyboard without the need to press enter WHICH IS DEFINED ON THE
   STANDARD (independant of the used compiler and machine).


I know that's a lot of questions to answer but that would be VERY
appreciated.

Please reply by mail. Thank you.

-- 

Benoit Rochefort
jk791840@er.uqam.ca

-- 

Benoit Rochefort
jk791840@er.uqam.ca





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

* Re: about abstract data types and pointer
  1996-09-21  0:00 about abstract data types and pointer Benoit Rochefort
  1996-09-24  0:00 ` Matthew Heaney
  1996-09-24  0:00 ` Norman H. Cohen
@ 1996-09-24  0:00 ` Stephen Leake
  2 siblings, 0 replies; 5+ messages in thread
From: Stephen Leake @ 1996-09-24  0:00 UTC (permalink / raw)



Benoit Rochefort wrote:
> 
> Hi! I'm new with ADA but i'm a programmer since a long time.
> 
> I use the ADA83 standard (but with the gnat compiler than can compile
> ADA95).

Most of what you want is available in Ada95, but not in Ada83. See the
Ada95 reference manual and rational, on-line at:
http://lglsun.epfl.ch/Ada/

Another good reference is "Ada as a Second Language", by Norman Cohen,
available at good bookstores.

> 1- Is there a way to define a procedure type like in MODULA-2 in
>    a way such:
> 
>      TYPE PROC IS PROCEDURE(IN E:ELEMENT);

only in Ada 95 (since you don't want Ada83 generics). See LRM 3.10,
access to subprogram types.
 
> 2- Is there a way to totally hide the representation of an abstract
>    data type in the specification package? 

This is possible in Ada 83:

package Abstraction is
   type Hidden is private;

private
   type Totally_Hidden;
   type Hidden is access Totally_Hidden;
end Abstraction;

package body Abstraction is
   type Totally_Hidden is
   record
      ...
   end record;
end Abstraction;

The full declaration of type Totally_Hidden can be defered to the body,
since the clients of Abstraction only need to know that type Hidden is a
pointer.

> 
> 3- Is there a way to "add" or change attributes to a new type that i
>    have elaborated as an abstract data type (in a package).
> 
>    Ex: L'First could have another meaning here if L is a List.
>        List'Nb could denote the number of elements of a List L.
> 
>    I know i can (and sure i must) code these as functions but i just
>    want to use them as attribute; which could be much more natural here.
> 
>    Is that what RENAMES is for?

In Ada95, the compiler vendor can define new attributes, and you can
provide new functions for any defined attributes, but you can't define
your own new attributes.

In Ada83, you cannot provide new functions for attributes.

> 4- Is there a way to overload the ":=" operator if List is a
>    limited private type?

Not in Ada83. In Ada95, you can derive a type from Controlled, and then
define the subprograms Initialize, Adjust, and Finalize, which are
called at appropriate times during assignment. This turns out to be
better structured than directly overloading ":=". See Chapter 12.5 in
"Ada as a Second Language", or LRM 7.6

> 5- Do the ADA83 standard specify that the deallocation of memory is
>    automatically done? Or must I ensure that my program does it?

Ada83 and Ada95 both allow the compiler vendor to implement garbage
collection (I don't know of any that do). You can implement your own via
Unchecked_Deallocation.

> 
> 6- Now the easy one.
>    Is there a function or procedure to read a character on the
>    keyboard without the need to press enter WHICH IS DEFINED ON THE
>    STANDARD (independant of the used compiler and machine).

No. In Ada95, Text_IO.Get_Immediate is recommended (but not required) to
have this behaviour. I believe the latest version of gnat implements it
this way on most platforms.

> 
> I know that's a lot of questions to answer but that would be VERY
> appreciated.

No problem; I learned a lot from reading this group, and I'm pleased to
be able to return something.

> 
> Please reply by mail. Thank you.

Sorry, if you want to benefit from the community, you have to be a
member!

> Benoit Rochefort
> jk791840@er.uqam.ca

-- 
- Stephe
Stephen.Leake@gsfc.nasa.gov




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

* Re: about abstract data types and pointer
  1996-09-21  0:00 about abstract data types and pointer Benoit Rochefort
  1996-09-24  0:00 ` Matthew Heaney
@ 1996-09-24  0:00 ` Norman H. Cohen
  1996-09-24  0:00 ` Stephen Leake
  2 siblings, 0 replies; 5+ messages in thread
From: Norman H. Cohen @ 1996-09-24  0:00 UTC (permalink / raw)
  To: jk791840; +Cc: ncohen


Benoit Rochefort wrote:

> I use the ADA83 standard (but with the gnat compiler than can compile
> ADA95).

That's unfortunate, because most of your concerns are addressed in the
Ada-95 revision.  Since you have a compiler that compiles the current
version of the language, why restrict yourself to the old version?

> 1- Is there a way to define a procedure type like in MODULA-2 in
>    a way such:
> 
>      TYPE PROC IS PROCEDURE(IN E:ELEMENT);
> 
>    Or like in C where we can define a pointer to such a fonction?
>    (how can i define an access type in ADA that "points" to such
>    a procedure?)

Yes.  As in C, you use a pointer to the subprogram.  (Pointers in Ada
are called access values.)  The syntax is:

   type Proc is access procedure (E: in Element);

However, this feature is not available in Ada 83.  In Ada 83 you must
use generic templates or, if your compiler lets you, certain unportable
hacks involving the manipulation of addresses.

>    Please don't suggest me to use a generic proc, because it's NOT
>    what i want; Map could be called for MANY procedures, not just one
>    for each instance of a List. example:
> 
>      Map(L, Put);    -- Print each element on stdout.
>      Map(L, CountE); -- Count the number of elements (side effect).
>      Map(L, Draw);   -- Draw the value of L on a graph.

Sorry, too late, I already suggested it. ;-)  But the objection you
describe is not convincing.  Once you write the template

   generic
      with procedure Map_One_Element (E: in Element);
   procedure Map (L: in List);

you can create instances appropriate for each call site:

   procedure Put_Elements is new Map (Map_One_Element => Put);
   procedure Count_Elements is new Map (Map_One_Element => CountE);
   procedure Draw_Elements is new Map (Map_One_Element => Draw);

You can even bundle the instantiation with the call by using a block
statement:

   declare
      procedure Put_Elements is new Map (Map_One_Element => Put);
   begin
      Put_Elements (L);
   end;

>    Count could be written like this:
>
>    FUNCTION Count(L: IN List) RETURN Natural IS
...
>    END Count;

Well, you can't quite do THAT in Ada, since you declared Map as taking a
procedure with a parameter of type List and NO result.  (Modula-2
doesn't let you do that either.)  But I presume that this inconsistency
in your post was unintentional.
 
> 2- Is there a way to totally hide the representation of an abstract
>    data type in the specification package? I really don't understand
>    why we should specify the representation of such a type in the
>    PRIVATE part of the specification package (except for compiler
>    needs -- that is simpler for the compiler :-) ).

It's an efficiency issue.  After the package specification is compiled,
clients of the package specification can be efficiently compiled, even
before the package body is compiled.  For example, a client might
contain an assignment statement copying a value of the private type.  If
the compiler can deduce the length of the value from the full
declaration in the private part of the package specification, it can
choose an appropriate machine instruction.

However, if the definition of the data structure in the private part of
the package specification bothers you, you can declare a private type to
be implemented as a pointer to some type whose declaration is deferred
to the package body:

   package P is
      type T is private;
      ...
   private
      type T_Node;   -- Incomplete declaration
      type T is access T_Node;
   end P;

   package body P is
      type T_Node is ...;  -- Full declaration
      ...
   end P;

Both Ada 83 and Ada 95 allow you to defer the full declaration of T in
this way.  The mechanism is identical to the Modula-2 opaque type.  The
only difference is that Ada gives you the choice between fastidiousness
about the aesthetics of data abstraction and saving the cost of an extra
level of pointer indirection.
       
> 3- Is there a way to "add" or change attributes to a new type that i
>    have elaborated as an abstract data type (in a package).
> 
>    Ex: L'First could have another meaning here if L is a List.
>        List'Nb could denote the number of elements of a List L.
> 
>    I know i can (and sure i must) code these as functions but i just
>    want to use them as attribute; which could be much more natural here.

Sorry, you have to use the function-call notation (which I find every
bit as natural).

>    Is that what RENAMES is for?

No, you can rename certain attributes as functions, but you can't do the
opposite.
 
> 4- Is there a way to overload the ":=" operator if List is a
>    limited private type?
> 
>    ex:
> 
>        L1 := L2;
> 
>    could make a copy of all elements of L1 into L2. This is much more
>    natural than
> 
>        Copy(L1, L2);

In Ada 83 you just write the Copy procedure and live with the slightly
less natural notation.  In Ada 95, you declare list to be a nonlimited
"controlled type" (i.e., a type derived from the type
Ada.Finalization.Controlled) and override its Adjust procedure.  This is
a procedure automatically invoked with the target object of an
assignment just after the assignment takes place.  In your example, the
overriding version of Adjust would replace the pointer that the
assignment copied into a List object with a pointer to a pure copy of
the list created by the procedure.

> 5- Do the ADA83 standard specify that the deallocation of memory is
>    automatically done? Or must I ensure that my program does it?
> 
>    ex:
> 
>    PROCEDURE Test IS
>      L: List;
>    BEGIN
>      Add(L, 3);
>      Add(L, 4);
>    END Test;
> 
>    If L is a pointer to some NEW element created; will it be freed
>    automaticaly on return of the PROCEDURE Test? Or must i provide
>    an operation to free memory (like Dispose)?

Both Ada 83 and Ada 95 explicitly ALLOW automatic garbage collection,
but do not require it.  No Ada-83 compiler supports it, and neither does
GNAT, but Ada-95 compilers targeted to the Java virtual machine do.

Ada-95 controlled types provide essentially the same functionality as
C++ destructors.  In addition to the Adjust procedure I already
described, there is a Finalize procedure that can be overridden when the
type Ada.Finalization.Controlled is extended.  It is invoked
automatically just before an object is about to cease to exist.  In the
example above, L ceases to exist upon return from Test, so just before
Test returns, the Finalize procedure for type List is invoked with L as
a parameter.  This version of Finalize would traverse the list and free
every cell (or perhaps just decrement a reference count that Adjust
increments, and free the list cells when the reference count falls to
zero).

> 6- Now the easy one.
>    Is there a function or procedure to read a character on the
>    keyboard without the need to press enter WHICH IS DEFINED ON THE
>    STANDARD (independant of the used compiler and machine).

Again, there is no standard mechanism in Ada 83.  Ada 95 provides the
procedure Ada.Text_IO.Get_Immediate for this purpose.  (This procedure
was not fully supported in early releases of GNAT, but I believe it has
been supported in more recent releases.)

-- 
Norman H. Cohen
mailto:ncohen@watson.ibm.com
http://www.research.ibm.com/people/n/ncohen




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

* Re: about abstract data types and pointer
  1996-09-21  0:00 about abstract data types and pointer Benoit Rochefort
@ 1996-09-24  0:00 ` Matthew Heaney
  1996-09-24  0:00 ` Norman H. Cohen
  1996-09-24  0:00 ` Stephen Leake
  2 siblings, 0 replies; 5+ messages in thread
From: Matthew Heaney @ 1996-09-24  0:00 UTC (permalink / raw)



In article <R.3244B03D.32EA@er.uqam.ca>, jk791840@er.uqam.ca wrote:

>Reposting article removed by rogue canceller.
>
>Hi! I'm new with ADA but i'm a programmer since a long time.

Ada is not an acronym, thus you need not capitalize its name.  The language
was named in honor of Ada Loveless, the assistant of Charles Babbage, and
the world's first programmer.

>
>I use the ADA83 standard (but with the gnat compiler than can compile
>ADA95).
>
>I have some questions to start with:
>
>1- Is there a way to define a procedure type like in MODULA-2 in
>   a way such:
>
>     TYPE PROC IS PROCEDURE(IN E:ELEMENT);
>
>   Or like in C where we can define a pointer to such a fonction?
>   (how can i define an access type in ADA that "points" to such
>   a procedure?)

(Note that my Ada 95 syntax is a little shaky...)

   type Procedure_Access is access procedure (E : in Element);

For example,

   procedure Process (The_Element : in Element);

   PA : Procedure_Access := Process'Access;

To invoke procedure Process through the access object:
 
   E : Element := ...;
   PA (E);


>   This would be very helpful in the case of a list type (for example),
>   having an operation defined like that:
>
>     PROCEDURE Map(L: IN List; P: PROC);
>
>   Which map the P Procedure to each element of the list. P would
>   obviously cause a side effect (like printing an element).
>
>   OR can I define procedure Map something like that:
>
>     PROCEDURE Map(L: IN List; P: PROCEDURE(IN ELEMENT));
>
>   Please tell me how i can call the p procedure passed as a parameter
>   to Map in the code; maybe something like:


   type List is private;

   type Procedure_Access is access procedure (E  : in Element);

   procedure Map (L : in List; P : in Procedure_Access);

>
>     P(E);

That is how you call the actual argument P, in fact.

>   Please don't suggest me to use a generic proc, because it's NOT
>   what i want; Map could be called for MANY procedures, not just one
>   for each instance of a List. example:
>
>     Map(L, Put);    -- Print each element on stdout.
>     Map(L, CountE); -- Count the number of elements (side effect).
>     Map(L, Draw);   -- Draw the value of L on a graph.
>
>   Of course; Draw and Put have the side effect of "writing".

   procedure Put (E : Element);
   procedure CountE (E: Element);
   procedure Draw (E : Element);

   L : List;

   Map (L, Put'Access);
   Map (L, CountE'Access);
   Map (L, Draw'Access);

>
>   Count could be written like this:
>
>   FUNCTION Count(L: IN List) RETURN Natural IS
>     Nb : Natural := 0;
>     PROCEDURE CountE IS
>     BEGIN
>       Nb := Nb + 1;
>     END CountE;
>   BEGIN
>     Map(L, CountE);
>     RETURN Nb;
>   END Count;

I'm not sure about the exact legality rules for doing this.

Either

Map (L, CountE'Access);

is allowed, or if not, then

Map (L, Count'Unchecked_Access);

for use when you know something about the lifetime of the subprogram you're
accessing that the compiler doesn't.


>2- Is there a way to totally hide the representation of an abstract
>   data type in the specification package? I really don't understand
>   why we should specify the representation of such a type in the
>   PRIVATE part of the specification package (except for compiler
>   needs -- that is simpler for the compiler :-) ). Even in C we
>   can hide the representation of an abstract data type from the 
>   header file. Why not in ADA which is supposed to be better for 
>   the information hiding principle?

Well, I'm not sure what you mean there about C.  For example, in C I might do

   typedef struct StackRep* Stack;

to declare a _pointer_ to stack in the header, and hide the representation
of its structure in the body.  That's a bit different from what you said,
though.

And yes, you can so the same thing in Ada:

package Stacks is

   type Stack is private;
   ...
private

   type Stack_Representation;
   type Stack is access Stack_Representation;

end Stacks;

This allows one to defer the implementation of the Stack_Representation to
the package body.

However, this is an probably an improper use of access types.  One should
use access types because the abstraction itself requires reference rather
than value semantics.

To implement an abstract data type as an access type to _really_ hide the
representation is a not such a great idea (at least in Ada 83), because of
all the attendant problems one has with  memory leaks.

And no, putting the representation in the private region of the
specification does *not* violate information hiding (nor does it violate
information hiding in C++ or Eiffel or...).  Information hiding does not
mean where the full view of the type appears (I could just read the body in
that case), but rather how clients are able to manipulate the abstraction.


>3- Is there a way to "add" or change attributes to a new type that i 
>   have elaborated as an abstract data type (in a package).
>
>   Ex: L'First could have another meaning here if L is a List.
>       List'Nb could denote the number of elements of a List L.

None that I know of (though I think it's a good idea).  The only attributes
I think you can change in Ada 95 are the T'Input and T'Output, for use with
Streams_IO.

>
>   I know i can (and sure i must) code these as functions but i just
>   want to use them as attribute; which could be much more natural here.
>
>   Is that what RENAMES is for?

A great technique that should be in every Ada programmer's arsenal!  It's
Ada's alias feature.  For example, I can rename elements of a record if the
dot notation gets out of hand:

package Bounded_Stacks is

   type Stack (Max_Depth : Positive) is limited private;

   procedure Push (The_Item : in Item; On : in out Stack);

private

   type Item_Array  is array (Positive range <>) of Item;

   type Stack (Max_Depth : Positive) is
      record
         The_Items : Item_Array (1 .. Max_Depth);
         The_Depth : Natural := 0;
      end record;

end Bounded_Stacks;

...

   procedure Push (The_Item : in Item; On : in out Stack) is

      The_Stack : Stack renames On;   -- gives The_Stack a better name than "On"

      The_Depth : Natural renames The_Stack.The_Depth;
   begin
      The_Depth := The_Depth + 1;
      The_Stack.The_Items (The_Depth) := The_Item;
   end;

compare that to this

   procedure Push (The_Item : in Item; On : in out  Stack) is
   begin
      On.The_Depth := On.The_Depth + 1;
      On.The_Items (On.The_Depth) := The_Item;
   end;

So you can rename objects.  But you can also rename subprograms

   with Bounded_Stacks;
   package P is

      function Push (The_Item : in Item;  On : in out Stack)
      renames Bounded_Stacks.Push;

and rename exceptions (or any other object, really)

   with IO_Exceptions;
   package Text_IO is

      type File_Type is limited private;
      ...
      Use_Error : exception renames IO_Exceptions.Use_Error;
   end;

You can even rename a task entry as a procedure:

   task T is
      entry E (The_Item : in Item);
   end T;

   procedure Put (The_Item : in Item) renames T.E;

You  can even rename an array slice as an object:

   procedure P (S : String) is
      O : String renames S (1..4);

Cool, huh?


>4- Is there a way to overload the ":=" operator if List is a
>   limited private type?
>
>   ex:
>
>       L1 := L2;
>
>   could make a copy of all elements of L1 into L2. This is much more
>   natural than
>
>       Copy(L1, L2);

Technically, the assignment operator ":=" isn't really an operator (I don't
know the exact reasons, you'll have to ask the language theory gods...). 
You can't "add" an "assignment operator" to a type that is already declared
as limited private, but you can get control of assignment if your type is
(non-limited) private by deriving from type Finalization.Controlled:

   with Finalization;
   package Lists is
  
      type List is private;
      ...
   private

      type Node;
      type Node_Access is access Node;

      type List is new Finalization.Controlled with
          record
              Head : Node_Access;
         end record;

   end Lists;

That way 

   L1 := L2;

can have value (copy) semantics rather than reference semantics.


>
>5- Do the ADA83 standard specify that the deallocation of memory is
>   automatically done? Or must I ensure that my program does it?

You must make sure no memory leaks occur.  There are pragmatic reasons for
not mandating that Ada herself take care of the clean-up, specifically that
the use of a garbage collector can cause unpredictable run-time behavior.

But you don't have to worry about memory leaks if you inherit from
Finalization.Controlled, which will invoke a subprogram to finalize the
object (reclaim its storage, in this case) when its lifetime ends.

>
>   ex:
>   
>   PROCEDURE Test IS
>     L: List;
>   BEGIN
>     Add(L, 3);
>     Add(L, 4);
>   END Test;
>
>   If L is a pointer to some NEW element created; will it be freed
>   automaticaly on return of the PROCEDURE Test? Or must i provide
>   an operation to free memory (like Dispose)?

If List is implemented as inheriting from Controlled, then all is well, and
you (the client of L) don't have to free the memory yourself.

>   The next problem is that probably i would have allocated some extra
>   memory for the elements of L; will it be also freed automaticaly?
>   Some language do it (SML, SMALLTALK), some don't (MODULA-2, C, C++)
>
>   However, we could define a destructor in C++; is it possible in ADA
>   that the destructor would be automatticaly called when the
>   object fall out of scope?

The "deconstructor" is a primitive operation (called Finalize) of types
that inherit from Controlled,  that gets invoked automatically when the
scope of an object closes.


>6- Now the easy one.
>   Is there a function or procedure to read a character on the
>   keyboard without the need to press enter WHICH IS DEFINED ON THE
>   STANDARD (independant of the used compiler and machine).

Yes.  It's called Text_IO.Get_Immediate.

>I know that's a lot of questions to answer but that would be VERY
>appreciated.
>
>Please reply by mail. Thank you.

Your very welcome!  Keep the posts a-comin'!

>Benoit Rochefort
>jk791840@er.uqam.ca

Matt

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




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

end of thread, other threads:[~1996-09-24  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-21  0:00 about abstract data types and pointer Benoit Rochefort
1996-09-24  0:00 ` Matthew Heaney
1996-09-24  0:00 ` Norman H. Cohen
1996-09-24  0:00 ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
1996-09-21  0:00 Benoit Rochefort

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