From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,MSGID_SHORT autolearn=no autolearn_force=no version=3.4.5-pre1 Date: 26 Sep 92 21:08:29 GMT From: gvls1!aviary!dmarshal@louie.udel.edu (Dave Marshall) Subject: Re: converting (limited) private types? Message-ID: <1750@aviary.Stars.Reston.Unisys.COM> List-Id: In article <1a0ijeINNfei@huon.itd.adelaide.edu.au>, andrewd@cs.adelaide.edu.au (Andrew Dunstan) writes: [some stuff deleted] > Trouble is, file types in Ada are limited private. So you can't assign > to objects of the type. So you can't make a stack of them. > > While pondering this, an evil thought came into my head. Before succumbing > to my better feelings, I quickly dialled up the university and tried the > following: > > with text_io; use text_io; > with unchecked_conversion; > procedure file_conv is > Ack! A totally UNJUSTIFIED use of UNCHECKED_CONVERSION! Flagellate yourself thrice daily for a week. [remainder of evil program deleted] > > What I want to know is: > > (1) APART from any righteous feelings about breaking the language's > privacy mechanism, are there any good reasons for not doing something > like this? Yes. Don't used UNCHECKED_CONVERSION if you don't have to. > > (2) Is there a better way to do it, one which does not rely on this > sort of hack? Yes. Access types. A simple example follows at the end of this response. > > (3) How can I close the damned file? (I've thought of a really disgusting > way, which oddly enough should be reasonably portable, but I certainly won't > publish it on the net.) Maybe that's for the best. :) [No chops-busting intended.] What follows is my trivial example of using access types to keep track of limited private types. In it, I keep an array of FILE_TYPE while I create files, write to them, and close them. Creating a stack package, or even using some other generic stack package, ought to be equally trivial. Here it is: with TEXT_IO; procedure FILE_TEST is type FILE_POINTER is access TEXT_IO.FILE_TYPE; type POINTERS is array ( INTEGER range <>) of FILE_POINTER; MY_ARRAY : POINTERS(1..5); SOME_NAMES : constant array (INTEGER range 1..5) of STRING(1..8) := ( "myfile.1", "myfile.2", "myfile.3", "myfile.4", "myfile.5"); begin for I in MY_ARRAY'RANGE loop MY_ARRAY(I) := new TEXT_IO.FILE_TYPE; TEXT_IO.PUT_LINE( "Creating file" & INTEGER'IMAGE(I)); TEXT_IO.CREATE ( MY_ARRAY(I).all, NAME => SOME_NAMES(I)); end loop; for J in MY_ARRAY'RANGE loop TEXT_IO.PUT_LINE ( "Writing to file" & INTEGER'IMAGE(J)); TEXT_IO.PUT_LINE ( MY_ARRAY(J).all, "I am file" & INTEGER'IMAGE(J)); end loop; for K in MY_ARRAY'RANGE loop TEXT_IO.PUT_LINE ( "Closing file" & INTEGER'IMAGE(K)); TEXT_IO.CLOSE ( MY_ARRAY(K).all); end loop; end FILE_TEST; Apologies to purists everywhere who take offense at my sloppiness in the declarative section. Have a nice day. -- Dave Marshall dmarshal@stars.reston.unisys.com