comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: type String_Type array(Integer range <>) of Character;
Date: Fri, 16 Apr 2010 13:56:03 -0700 (PDT)
Date: 2010-04-16T13:56:03-07:00	[thread overview]
Message-ID: <2dab1630-4abd-4d5b-a286-6fdbd18ce19c@b33g2000yqc.googlegroups.com> (raw)
In-Reply-To: hqahb1$tnm$1@news.eternal-september.org

On Apr 16, 1:29 pm, "J-P. Rosen" <ro...@adalog.fr> wrote:
> stefan-lu...@see-the.signature a écrit :> Convert S into a variable of type String_Type with String-compatible
> > bounds, and then convert that variable into a String:
>
> >     S : String_Type := "Abc.";
> >     T : String_Type(1 .. S'Length) := S;
> >     U : String(1 .. S'Length);
> >   begin
> >      U := String(T); Put_Line(U);  -- or just Put_Line(String(T));
>
> No need for intermediate copy, just use an appropriate subtype:
>
>    S : String_Type := "Abc.";
>    T : String(1..4);
>    subtype Slide is String_type (T'Range);
> begin
>    T := String(Slide(S));

You don't even need two type conversions:

     S : String_Type := "Abc.";
     subtype String_Subtype is String(1..S'Length);
     T : String_Subtype;
--   T : String(1..4);       -- this works too
   begin
     T := String_Subtype(S);

Sometimes reading the manual helps (especially if you know where to
look).  4.6(37-38) says about array type conversions:

* If the target subtype is a constrained array subtype, then a check
is made that the length of each dimension of the value of the operand
equals the length of the corresponding dimension of the target
subtype. The bounds of the result are those of the target subtype.
* If the target subtype is an unconstrained array subtype, then the
bounds of the result are obtained by converting each bound of the
value of the operand to the corresponding index type of the target
type. For each nonnull index range, a check is made that the bounds of
the range belong to the corresponding index subtype.

So if we're converting to a constrained subtype (like String_Subtype),
it just makes sure the length is the same on each dimension; while if
we're converting to an unconstrained subtype (like String), then each
bound is expected to be the same.  So we solve the problem by making
the type conversion target a constrained subtype.

                                   -- Adam




  parent reply	other threads:[~2010-04-16 20:56 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-16 19:52 type String_Type array(Integer range <>) of Character; Warren
2010-04-16 21:09 ` stefan-lucks
2010-04-16 20:29   ` J-P. Rosen
2010-04-16 20:53     ` Charmed Snark
2010-04-16 20:56     ` Adam Beneschan [this message]
2010-04-19 15:27       ` Warren
replies disabled

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