comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: strings and multidimensional arrays
Date: Mon, 13 Oct 2003 17:05:45 GMT
Date: 2003-10-13T17:05:45+00:00	[thread overview]
Message-ID: <3F8ADB3D.5010000@comcast.net> (raw)
In-Reply-To: JMoib.8968$dn6.4651@newsread4.news.pas.earthlink.net

Jeffrey Carter wrote:

 > Is GNAT wrong, or is it unnecessary magic? I've been doing this since
 >  1984. I know you can't have an array of indefinite elements (an
 > unconstrained array type, such as String, or a type with unknown
 > discriminants), but unconstrained records types are not indefinite.

Very interesting.  I see no problem with your package as written, in
fact I see no problem created objects of the array type.  (The
discriminants will all be assigned the default value One.)  But I
expected that a record assignment to an element that changed the
discriminant would raise Constraint_Error.  However, when I created an
instance of your array type and assigned to it this is what I got (from 
GNAT):

----------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Float_Text_IO;
procedure Disc_Test is

   package Test_Unconstrained_Record_Array is
     type Disc is (One, Two, Five);

     type Unconstrained_Record (D : Disc := One) is record
       case D is
       when One =>
          I : Integer;
       when Two =>
          C : Character;
       when Five =>
          F : Float;
       end case;
     end record;

     type Thing is array (Positive range <>, Positive range <>) of
                 Unconstrained_Record;

   end Test_Unconstrained_Record_Array;

   package TURA renames Test_Unconstrained_Record_Array;
   package TURA_IO is new Ada.Text_IO.Enumeration_IO(TURA.Disc);

   TT: TURA.Thing(1..5, 1..5);

begin

   TT(1,1) := (TURA.One, 1);
   TT(2,2) := (TURA.Two, '2');
   TT(5,5) := (TURA.Five, 5.0);
   for I in 1..5 loop
     Put(" TT(" & Integer'Image(I)(2) & ',' & Integer'Image(I)(2) & ") 
is ");
     Put("( D => "); TURA_IO.Put(TT(I,I).D);
     case TT(I,I).D is
       when TURA.One =>
         Put(" I =>"); Put(Integer'Image(TT(I,I).I));
       when TURA.Two =>
         Put(" C =>"); Put(Character'Image(TT(I,I).C));
       when TURA.Five =>
         Put(" I =>"); Ada.Float_Text_IO.Put(TT(I,I).F,2,1,0);
     end case;
     Put_Line(").");
   end loop;
end Disc_Test;

-----------------------------------------------------------------------------

E:\Ada\Test>disc_test
disc_test
  TT(1,1) is ( D => ONE I => 1).
  TT(2,2) is ( D => TWO C =>'2').
  TT(3,3) is ( D => ONE I => 2013340188).
  TT(4,4) is ( D => ONE I => 38010744).
  TT(5,5) is ( D => FIVE I => 5.0).

I'm not going to assert that this is wrong. That would be for the ARG as
a whole to do.  The key paragraphs of the standard are 3.6(14): "An 
array_type_definition defines an array type and its first subtype.
For each object of this array type, the number of indices, the type and
position of each index, and the subtype of the components are as in the
type definition[; the values of the lower and upper bounds for each
index belong to the corresponding index subtype of its type, except for
null arrays (see 3.6.1)]."  This is the paragraph that implicitly sets 
the discriminants for uninitialized components.

And 3.6(11): "Within the definition of a nonlimited composite type (or a 
limited composite type that later in its immediate scope becomes 
nonlimited -- see 7.3.1 and 7.5), if a component_definition contains the 
reserved word aliased and the type of the component is discriminated, 
then the nominal subtype of the component shall be constrained."

The first quote implies that the subtype of the components can't be 
changed, the second implies they can be unless the components are 
declared as aliased.  (Adding the wrapper type allows you to declare the 
components aliased.)  I'll wait to see what Bob Duff, Randy, and 
possibly Tucker have to say.  Certainly this is not documented as a 
change from Ada 83 in the AARM.  But even if it wasn't intended as 
legal, it is a nice feature. ;-)

-- 
                                         Robert I. Eachus

"Quality is the Buddha. Quality is scientific reality. Quality is the
goal of Art. It remains to work these concepts into a practical,
down-to-earth context, and for this there is nothing more practical or
down-to-earth than what I have been talking about all along...the repair
of an old motorcycle."  -- from Zen and the Art of Motorcycle
Maintenance by Robert Pirsig




  reply	other threads:[~2003-10-13 17:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-12  4:05 strings and multidimensional arrays Andrew
2003-10-12  5:24 ` Andrew
2003-10-12  6:27   ` tmoran
2003-10-12 14:16   ` Robert I. Eachus
2003-10-12 19:34     ` Jeffrey Carter
2003-10-13  0:02       ` Robert I. Eachus
2003-10-13  3:14         ` Jeffrey Carter
2003-10-13 17:05           ` Robert I. Eachus [this message]
2003-10-13 18:49             ` Martin Dowie
2003-10-13 21:47               ` Mark Lorenzen
2003-10-14  1:10             ` Jeffrey Carter
2003-10-14 15:29               ` Robert I. Eachus
2003-10-12 21:01     ` Andrew
2003-10-13 13:59 ` Rodrigo Garcia
  -- strict thread matches above, loose matches on Subject: below --
2003-10-14  6:24 christoph.grein
replies disabled

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