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.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,66423c08b54dd4a3,start X-Google-Attributes: gid103376,public From: Benoit Rochefort Subject: about abstract data types and pointer Date: 1996/09/21 Message-ID: <3244B03D.32EA@er.uqam.ca>#1/1 X-Deja-AN: 184503217 distribution: world content-type: text/plain; charset=us-ascii organization: Universite du Quebec a Montreal mime-version: 1.0 reply-to: jk791840@er.uqam.ca newsgroups: comp.lang.ada x-mailer: Mozilla 3.0b7Gold (Win95; I) Date: 1996-09-21T00:00:00+00:00 List-Id: 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