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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,413069df09dfed26 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Nick Roberts Newsgroups: comp.lang.ada Subject: Re: Converting Date: Tue, 28 Dec 2004 22:52:38 +0000 Message-ID: References: <1104260481.548435.238280@f14g2000cwb.googlegroups.com> Content-Type: text/plain; charset=us-ascii X-Trace: individual.net KRxE+ZUQEfPA82uvt6Hl7AAjKfHMGGJtRqYIjHFcPlP3YGofg= X-Orig-Path: not-for-mail User-Agent: Gemini/1.45d (Qt/3.3.2) (Windows-XP) Xref: g2news1.google.com comp.lang.ada:7276 Date: 2004-12-28T22:52:38+00:00 List-Id: conradwt@runbox.com wrote: > Hi, I was wondering, could someone tell me the best way to convert a > string literal to a subtype. For example, > > subtype A_Type.String is Standard.String; > subtype B_Type_String is A_Type.String; > > Now, I would like to convert a string literal to B_Type_String. BTW, the > method that I'm calling requires the type B_Type_String. As another replier says, the two subtypes you illustrate are both of the same type (Standard.String), so no explicit conversion is required between values or objects of the two subtypes (A_Type.String and B_Type_String). It is illegal to declare two overloaded subprograms that differ only in the subtype of one of the parameters, immediately within the same delcartive region. This situation should be rejected by your compiler. For example: package Foo is procedure Bar (S: in out A_Type.String); procedure Bar (S: in out B_Type_String); -- illegal ... end; The reason why is that there could never be any way to call Bar unambiguously. I presume this is not your problem. It /is/ legal to declare two such overloaded subprograms in separate declarative regions. Since a declarative region must have a distinct full name if it has a name (and all declarative regions except a declare block must have a name, and a declare block can be given a name if it doesn't have one), it is always possible to diambiguate calls to these subprograms by using their full names. For example: package Foo is procedure Bar (S: in out A_Type.String); ... end; package Hum is procedure Bar (S: in out B_Type_String); ... end; ... S1, S2: B_Type_String; begin Foo.Bar(S1); Hum.Bar(S2); Note that these two calls are legal, since both S1 and S2 are of the type Standard.String (and that is all that matters). You can qualify a literal with a subtype name as follows: A_Type.String'("Hello") B_Type_String'("World") This doesn't change the fact that both literals are of the same type (and no actual conversion of any kind is performed). However, it might be useful for self-documentation of the code, and it might be useful if there is a chance that one of the subtypes will be changed in the future to have a different base type. Possibly there are further issues? -- Nick Roberts