From: Benoit Rochefort <jk791840@er.uqam.ca>
Subject: about abstract data types and pointer
Date: 1996/09/21
Date: 1996-09-21T00:00:00+00:00 [thread overview]
Message-ID: <R.3244B03D.32EA@er.uqam.ca> (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
next reply other threads:[~1996-09-21 0:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-09-21 0:00 Benoit Rochefort [this message]
1996-09-24 0:00 ` about abstract data types and pointer Norman H. Cohen
1996-09-24 0:00 ` Stephen Leake
1996-09-24 0:00 ` Matthew Heaney
-- strict thread matches above, loose matches on Subject: below --
1996-09-21 0:00 Benoit Rochefort
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox