From: gvls1!aviary!dmarshal@louie.udel.edu (Dave Marshall)
Subject: Re: converting (limited) private types?
Date: 26 Sep 92 21:08:29 GMT [thread overview]
Message-ID: <1750@aviary.Stars.Reston.Unisys.COM> (raw)
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
next reply other threads:[~1992-09-26 21:08 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1992-09-26 21:08 Dave Marshall [this message]
-- strict thread matches above, loose matches on Subject: below --
1992-09-29 20:00 converting (limited) private types? Robert I. Eachus
1992-09-27 20:07 cis.ohio-state.edu!news.sei.cmu.edu!ae
1992-09-26 2:40 munnari.oz.au!yoyo.aarnet.edu.au!news.adelaide.edu.au!cs.adelaide.edu.au!
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox