* package body T is
@ 1996-09-23 0:00 Chris Sparks
1996-09-24 0:00 ` Jon S Anthony
0 siblings, 1 reply; 2+ messages in thread
From: Chris Sparks @ 1996-09-23 0:00 UTC (permalink / raw)
> Date: Sat, 21 Sep 1996 23:19:25 -0400
> From: Benoit Rochefort <jk791840@ER.UQAM.CA>
> Subject: about abstract data types and pointer
>
> Hi! I'm new with ADA but i'm a programmer since a long time.
Welcome aboard!
> 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);
How about: type Proc is access procedure (E : in Element);
This is an Ada95 construct though!
> 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);
How about: P (E => <some variable of Element type>).all;
This is an Ada95 construct too!
> 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;
Remember though that Put, CountE, Draw must all have the same profile
as the subprogram pointer above.
This is an Ada95 stipulation!
> 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?
Do you mean using Ada95's abstract types or using a regular private type?
If you meant the later what you would do is the following:
package T is
type A is private;
private
type A_Stuff;
type A is access A_Stuff;
end T;
package body T is
type A_Stuff is new Integer;
end T;
> 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?
If you are asking if there are user defined attributes, then the answer is no :(
> 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);
Ada95 doesn't support overloading the ":=", however, if you look into the
Ada.Finalization (Initialize, Adjust, Finalize) example (Ada.Strings.Unbounded
body)
you will see how it is done. Also check the LRM. A full description here would
take
too much time.
> 5- Do the ADA83 standard specify that the deallocation of memory is
> automatically done? Or must I ensure that my program does it?
I hope I don't get flamed here. The answer is no and yes to both your
questions.
> 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?
If you want control over allocated memory look into storage pools and
finalization
stuff and merge the two. That is what I did to make my automatic garbage
collector.
> 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).
Have you looked into Ada.Text_IO.Get_Immediate? This may be the one you
need.
> I know that's a lot of questions to answer but that would be VERY
> appreciated.
My bill is in the mail. Just kidding! :-)
> Please reply by mail. Thank you.
>
> --
>
> Benoit Rochefort
> jk791840@er.uqam.ca
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: package body T is
1996-09-23 0:00 package body T is Chris Sparks
@ 1996-09-24 0:00 ` Jon S Anthony
0 siblings, 0 replies; 2+ messages in thread
From: Jon S Anthony @ 1996-09-24 0:00 UTC (permalink / raw)
In article <INFO-ADA%96092309195597@LISTSERV.NODAK.EDU> Chris Sparks <sparks@AISF.COM> writes:
> > 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);
>
> How about: P (E => <some variable of Element type>).all;
> This is an Ada95 construct too!
Well, that would either be:
P(E); -- (no need to explicitly dereference these anymore than any other
-- access type...)
or
P.all(E);
but NOT (as you have it)
P(E).all; -- ILLEGAL...
> Remember though that Put, CountE, Draw must all have the same profile
> as the subprogram pointer above.
> This is an Ada95 stipulation!
That's the same as for M2 or M3.
> > 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
Yes, it is an efficiency "hack"...
> Do you mean using Ada95's abstract types or using a regular private type?
> If you meant the later what you would do is the following:
["regular" example snipped]
Abstract types are no different. Tagged types are a little different:
package T is
type A is tagged private;
...
private
type A_Impl;
type A_Impl_Ref is access A_Impl;
type A is tagged record
Impl : A_Impl_Ref;
end record;
end T;
Now the dispatching is still available...
> > 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?
>
> If you are asking if there are user defined attributes, then the answer is no :(
Not quite. You can define your own 'Read 'Write, etc.
> If you want control over allocated memory look into storage pools
> and finalization > stuff and merge the two. That is what I did to
> make my automatic garbage collector.
Or, use limited types with unknown discriminants. Define your own
constructor returning a reference which uses your own storage pool
definition which in turn is set up with roots for "real" GC (no
finalization stuff). You also need a couple more tricks to guard
against "in limbo stack access values", but these will be application
specific...
/Jon
--
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178
617.484.3383
jsa@organon.com
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1996-09-24 0:00 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-09-23 0:00 package body T is Chris Sparks
1996-09-24 0:00 ` Jon S Anthony
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox