comp.lang.ada
 help / color / mirror / Atom feed
From: Nick Roberts <nick.roberts@acm.org>
Subject: Re: Converting
Date: Tue, 28 Dec 2004 22:52:38 +0000
Date: 2004-12-28T22:52:38+00:00	[thread overview]
Message-ID: <gemini.i9gfjq0029xp801ro.nick.roberts@acm.org> (raw)
In-Reply-To: 1104260481.548435.238280@f14g2000cwb.googlegroups.com

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



  parent reply	other threads:[~2004-12-28 22:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-28 19:01 Converting conradwt
2004-12-28 21:12 ` Converting Martin Dowie
2004-12-28 22:52 ` Nick Roberts [this message]
2004-12-29  4:43 ` Converting Jeffrey Carter
2004-12-29  5:06 ` Converting Lady Chatterly
2004-12-29 19:07 ` Converting Martin Krischik
replies disabled

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