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 X-Google-Attributes: gid103376,public From: Geert Bosch Subject: Re: array conversion - how to do? Date: 1997/06/28 Message-ID: <5p3qk2$8mk$1@gonzo.sun3.iaf.nl>#1/1 X-Deja-AN: 253458325 References: <5opej8$rv5$1@gonzo.sun3.iaf.nl> <33B3DE95.41EF@does.not.exist.com> Organization: La Calandre Infortunee Newsgroups: comp.lang.ada Date: 1997-06-28T00:00:00+00:00 List-Id: Geert says ``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. '' Robert replies: ``You worry about writing the loop, but a good compiler should be able to generate efficient code for that loop. After all the checks will all get eliminated, and then there is no reason to think the loop will generate code any different on a typical machine from the conversion. '' No, that is not the point. There is a fundamental problem why this conversion is really expensive on all Ada implementations while it should be free. Assume the following declarations: subtype ASCII_Character is Character range Character'Val (32) .. Character'Val (127); type String is array (Positive range <>) of Character; type ASCII_String is array (Positive range <>) of Character; procedure Error (Message : String); Storage_Msg : ASCII_String := "Out of storage"; The point is that to write the conversion in another way than using a typecast, I need to copy the entire string: -- Code to do Error (String (Storage_Msg)); declare Error_String : String (Storage_Msg'Range); begin for I in Error_String'Range loop Error_String (I) := Storage_Msg (I); end loop; Error (Error_String); end; My objection is not only that I need to write extra code, but that the net effect of that code is to accomplish nothing. Of course anybody facing this problem will create a function that does the conversion, but I really doubt if this function call will get eliminated by the compiler. Although this specific example might not be too good, there are very similar situations when using access-to-class-wide types in arrays. Now I think about it, it is possible to rather closely model typecasting for arrays by implementing array conversion as showed below. It is a pity that this generates lots of code with GNAT although all code could be eliminated. Are there any compilers that correctly compile this into a null function? Regards, Geert -- Array_Conversion provides a typecasting function for arrays generic type Source_Element is limited private; type Target_Element is new Source_Element; type Index is (<>); type Source_Array is array (Index range <>) of Source_Element; type Target_Array is array (Index range <>) of Target_Element; function Array_Conversion (S : Source_Array) return Target_Array; pragma Pure (Array_Conversion); with Unchecked_Conversion; function Array_Conversion (S : Source_Array) return Target_Array is subtype Fixed_Source is Source_Array (S'Range); subtype Fixed_Target is Target_Array (S'Range); function Convert is new Unchecked_Conversion (Fixed_Source, Fixed_Target); begin return Convert (Fixed_Source (S)); end Array_Conversion;