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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8bf89a9bbd99a65e,start X-Google-Attributes: gid103376,public From: Geert Bosch Subject: array conversion - how to do? Date: 1997/06/24 Message-ID: <5opej8$rv5$1@gonzo.sun3.iaf.nl>#1/1 X-Deja-AN: 252778537 Organization: La Calandre Infortunee Newsgroups: comp.lang.ada Date: 1997-06-24T00:00:00+00:00 List-Id: 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