comp.lang.ada
 help / color / mirror / Atom feed
* array conversion - how to do?
@ 1997-06-24  0:00 Geert Bosch
  1997-06-27  0:00 ` Wes Groleau
  1997-06-28  0:00 ` Robert I. Eachus
  0 siblings, 2 replies; 11+ messages in thread
From: Geert Bosch @ 1997-06-24  0:00 UTC (permalink / raw)



Here is a simple situation illustrating a problem I encountered
in using arrays of pointers to tagged objects. I have an
application that should only use printable ASCII characters in its
string processing. So I declare the following:
   
   subtype ASCII_Character is Character 
     range Character'Val (32) .. Character'Val (127);
   type ASCII_String is array (Positive range <>) of ASCII_Character;

Now this string is clearly convertible to the String type, since
the components have the same type, the ASCII_Character is only
more constrained. Thus when my application wants to output its
ASCII_String results, it uses:

   with Ada.Text_IO; use Ada.Text_IO;
   procedure Test is
      Warning_Message : ASCII_String := "Warning: only use 7-bit ASCII";
   begin
      ... 
      Put_Line (String (Warning_Message));
   end Test;

Although this is IMHO a solution with clear semantics, this is
not legal Ada because of the rule that array conversions are only
allowed between array types that have components of the same subtype.
The workaround is both ugly and inefficient using a typical Ada 
implementation:

   procedure Put_Line (Item : in ASCII_String) is
      Standard_String : String (Item'Range);
   begin
      for I in Item'Range loop
	 Standard_String (I) := Item (I);
      end loop;
   end Put_Line;

   ...  -- And so on for all subprograms that use strings

I could imagine why it would not be possible to convert String
to ASCII_String, since that conversion might need time-consuming
checks. The conversion in the example is completely safe though,
which is statically checkable for the compiler.

Actually in my real program where I use arrays of 
access-to-classwide types it really makes sense to convert 
arrays of access B'Class to arrays of access A'Class, when
B is an extension of A. 

In some situations it is really useful to use arrays of
(access-to) tagged types, but it is quite annoying that
I cannot convert arrays to more general types but need
to copy them or use Unchecked_Conversion.

Can somebody give me a reason why this limitation is
in the language? A good work-around would also be very
welcome.

Regards,
   Geert




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~1997-07-04  0:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-06-24  0:00 array conversion - how to do? Geert Bosch
1997-06-27  0:00 ` Wes Groleau
1997-06-27  0:00   ` Robert Dewar
1997-06-28  0:00     ` Geert Bosch
1997-07-03  0:00       ` Robert Dewar
1997-07-04  0:00         ` Geert Bosch
1997-07-04  0:00           ` Robert Dewar
1997-07-04  0:00         ` Geert Bosch
1997-06-28  0:00 ` Robert I. Eachus
1997-06-28  0:00   ` Geert Bosch
1997-06-30  0:00     ` Robert I. Eachus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox