comp.lang.ada
 help / color / mirror / Atom feed
* Converting
@ 2004-12-28 19:01 conradwt
  2004-12-28 21:12 ` Converting Martin Dowie
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: conradwt @ 2004-12-28 19:01 UTC (permalink / raw)


Hi, I was wondering, could someone tell me the best way to convert s
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.  Last but
least, I using the most recent version of gnat to do the compile.  If
anyone has any ideas as to the best way to resolve this issue and
reducing the number of code changes, I would be most appreciative.
Thanks in advance,

-Conrad




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

* Re: Converting
  2004-12-28 19:01 Converting conradwt
@ 2004-12-28 21:12 ` Martin Dowie
  2004-12-28 22:52 ` Converting Nick Roberts
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Martin Dowie @ 2004-12-28 21:12 UTC (permalink / raw)


conradwt@runbox.com wrote:
> Hi, I was wondering, could someone tell me the best way to convert s
> 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.  Last but
> least, I using the most recent version of gnat to do the compile.  If
> anyone has any ideas as to the best way to resolve this issue and
> reducing the number of code changes, I would be most appreciative.
> Thanks in advance,

Subtypes don't create new types, so if you have a string literal it is 
most likely to be of type Standard.String anyway (or it could be a 
Wide_String).

I only use subtypes if I'm also adding a constraint to the type (i.e. 
reducing the range of valid values it can take).



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

* Re: Converting
  2004-12-28 19:01 Converting conradwt
  2004-12-28 21:12 ` Converting Martin Dowie
@ 2004-12-28 22:52 ` Nick Roberts
  2004-12-29  4:43 ` Converting Jeffrey Carter
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Nick Roberts @ 2004-12-28 22:52 UTC (permalink / raw)


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



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

* Re: Converting
  2004-12-28 19:01 Converting conradwt
  2004-12-28 21:12 ` Converting Martin Dowie
  2004-12-28 22:52 ` Converting Nick Roberts
@ 2004-12-29  4:43 ` Jeffrey Carter
  2004-12-29  5:06 ` Converting Lady Chatterly
  2004-12-29 19:07 ` Converting Martin Krischik
  4 siblings, 0 replies; 6+ messages in thread
From: Jeffrey Carter @ 2004-12-29  4:43 UTC (permalink / raw)


conradwt@runbox.com wrote:

> Hi, I was wondering, could someone tell me the best way to convert s
> string literal to a subtype.  For example,
> 
> subtype A_Type.String is Standard.String;
> subtype B_Type_String is A_Type.String;

A string literal is a special form of an array aggregate. There need 
never be an explicit conversion from a literal to the type; the type of 
the literal must be determined from context, just as for any array 
aggregate.

In your example, there is only one string type: the type Standard.String.

C  : constant Standard.String := "This is a string literal";
V1 : A_Type.String := C;
V2 : B_Type_String := V1;

-- 
Jeff Carter
"Beyond 100,000 lines of code you
should probably be coding in Ada."
P. J. Plauger
26



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

* Re: Converting
  2004-12-28 19:01 Converting conradwt
                   ` (2 preceding siblings ...)
  2004-12-29  4:43 ` Converting Jeffrey Carter
@ 2004-12-29  5:06 ` Lady Chatterly
  2004-12-29 19:07 ` Converting Martin Krischik
  4 siblings, 0 replies; 6+ messages in thread
From: Lady Chatterly @ 2004-12-29  5:06 UTC (permalink / raw)


In article <1104260481.548435.238280@f14g2000cwb.googlegroups.com> conradwt@runbox.com wrote:
>
>subtype A_Type.String is Standard.String;
>subtype B_Type_String is A_Type.String;

String is a psychologist on the border of Canada, saying in ontario
wife abuse is against the law.

>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.  Last but
>least, I using the most recent version of gnat to do the compile.  If
>anyone has any ideas as to the best way to resolve this issue and
>reducing the number of code changes, I would be most appreciative.
>Thanks in advance,

Have you thought you were calling requires the type B type string?

--
Lady Chatterly

"Neither he nor I even have the time to read all the attention you
give us now. Only Lady Chatterly can keep up with the constant flow of
Yingarrhea." -- Daedalus




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

* Re: Converting
  2004-12-28 19:01 Converting conradwt
                   ` (3 preceding siblings ...)
  2004-12-29  5:06 ` Converting Lady Chatterly
@ 2004-12-29 19:07 ` Martin Krischik
  4 siblings, 0 replies; 6+ messages in thread
From: Martin Krischik @ 2004-12-29 19:07 UTC (permalink / raw)


conradwt@runbox.com wrote:

> Hi, I was wondering, could someone tell me the best way to convert s
> string literal to a subtype.  For example,
> 
> subtype A_Type.String is Standard.String;
> subtype B_Type_String is A_Type.String;

subtype don't need to be converted. Read:

Programming:Ada:Subtypes#type_vs._subtype

> 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.  Last but
> least, I using the most recent version of gnat to do the compile.  If
> anyone has any ideas as to the best way to resolve this issue and
> reducing the number of code changes, I would be most appreciative.
> Thanks in advance,

Read:

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Converting_Data 

Checked convertion works on arrays as well

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

end of thread, other threads:[~2004-12-29 19:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-28 19:01 Converting conradwt
2004-12-28 21:12 ` Converting Martin Dowie
2004-12-28 22:52 ` Converting Nick Roberts
2004-12-29  4:43 ` Converting Jeffrey Carter
2004-12-29  5:06 ` Converting Lady Chatterly
2004-12-29 19:07 ` Converting Martin Krischik

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