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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.107.29.80 with SMTP id d77mr30378357iod.27.1517667410455; Sat, 03 Feb 2018 06:16:50 -0800 (PST) X-Received: by 10.157.87.93 with SMTP id x29mr1205645oti.8.1517667410341; Sat, 03 Feb 2018 06:16:50 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!paganini.bofh.team!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!w142no987535ita.0!news-out.google.com!k194ni3660itb.0!nntp.google.com!w142no987533ita.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Sat, 3 Feb 2018 06:16:49 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.71.208.22; posting-account=QF6XPQoAAABce2NyPxxDAaKdAkN6RgAf NNTP-Posting-Host: 173.71.208.22 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <19b28472-5810-4306-b1b1-545cc7b6edcc@googlegroups.com> Subject: Re: Array of records with default values not propagating to array From: Jere Injection-Date: Sat, 03 Feb 2018 14:16:50 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader02.eternal-september.org comp.lang.ada:50288 Date: 2018-02-03T06:16:49-08:00 List-Id: On Saturday, February 3, 2018 at 6:06:52 AM UTC-5, Bojan Bozovic wrote: > When I defined a record, and set a default value for some of its componen= ts, these don't propagate when I make array of these records. Here is code = that can be compiled, but I wonder do I need to explicitly assign value for= 'others' in 'Initialize' procedure. This has nothing to do with Ada random= number generation, I might have as well used Ada.Numerics.Float_Random, or= the reusable code that was posted in another thread, with the same result,= so I will post in this separate thread. When I omit "others =3D>" in Initi= alize, I get compilation errors at these lines both on FSF compiler and Ada= Core GPL compiler on Windows. Is this intended behavior or I am missing som= ething? >=20 > with Ada.Numerics.Discrete_Random; > with Ada.Text_IO; > with Ada.Text_IO.Unbounded_IO; > with Ada.Strings; > with Ada.Strings.Unbounded; >=20 > procedure Tarot1 is >=20 > Maximum_Deck_Length: constant integer :=3D 78; > subtype Card_Position is Positive range 1..Maximum_Deck_Length; > type Card_Suits is (Cups,Pentacles,Swords,Wands); > type Trump_Values is new Natural range 0..21; > type Card_Values is (Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Page,Kn= ight,Queen,King,Ace); > type Card_Info (Trump: Boolean :=3D False) is record > -- we will skip giving name and divinatory meaning to cards - its just or= dinary assignment but for each card separately; > -- PROBLEM: Assignment here does nothing, so I have to assign these value= s in Initialize procedure below as well >=20 > Name : Ada.Strings.Unbounded.Unbounded_String :=3D Ada.Strings.Unbounded.= Null_Unbounded_String; > Divinatory_Meaning: Ada.Strings.Unbounded.Unbounded_String :=3D Ada.Strin= gs.Unbounded.Null_Unbounded_String; >=20 > case Trump is=20 > when True =3D> Trump_Value:Trump_Values; > when False =3D> Card_Value:Card_Values; > Suit_Value:Card_Suits; > end case; > end record; > type Deck is array (Card_Position) of Card_Info; > package Card_Position_Random is new Ada.Numerics.Discrete_Random (Result_= Subtype =3D> Card_Position); >=20 > procedure Initialize ( TDeck : in out Deck) is > Index: Card_Position :=3D 1; > begin > -- first lay 22 trump cards in a deck > for Index_1 in Trump_Values loop > TDeck (Index) :=3D (Trump =3D> True, Trump_Value =3D> Index_1, others = =3D> Ada.Strings.Unbounded.Null_Unbounded_String); > Index :=3D Index + 1; > end loop; > -- now lay 4 suits of 14 cards each; > for Suit_Index in Card_Suits loop > for Card_Index in Card_Values loop > TDeck (Index) :=3D (Trump =3D> False, Card_Value =3D> Card_Index, Suit= _Value =3D> Suit_Index, others =3D> Ada.Strings.Unbounded.Null_Unbounded_St= ring); > exit when Index=3DMaximum_Deck_Length; -- preventing CONSTRAINT_ERROR; > Index:=3DIndex+1; > end loop; > end loop; > end Initialize; >=20 > procedure Shuffle (TDeck : in out Deck) is > Position_Gen : Card_Position_Random.Generator; > Temporary_Data : Card_Info; > Index_Random : Card_Position; > begin > Card_Position_Random.Reset (Gen =3D> Position_Gen); > for Index in Card_Position loop > Index_Random :=3D Card_Position_Random.Random (Gen =3D> = Position_Gen); > Temporary_Data :=3D TDeck (Index); > TDeck (Index) :=3D TDeck (Index_Random); > TDeck (Index_Random) :=3D Temporary_Data; > end loop; > end Shuffle; > Tarot_Deck : Deck; > begin > Initialize(Tarot_Deck); > Shuffle(Tarot_Deck); > for Index in Card_Position loop -- just print the deck as shuffled > if Tarot_Deck(Index).Trump then > Ada.Text_IO.Put_Line("Major Arcana/Trump: "&Trump_Values'Image(Tarot_Dec= k(Index).Trump_Value)); > else > Ada.Text_IO.Put_Line("Minor Arcana: "&Card_Values'Image(Tarot_Deck(Index= ).Card_Value)&" of "&Card_Suits'Image(Tarot_Deck(Index).Suit_Value)); > end if; > end loop; > end Tarot1; >=20 > Thanks for help. The big issue I see is not incrementing Index in the first loop. As it stands, you simply keep reassigning trumps to the same card over and over again. Since the type is uninitialized and the next for loop cannot fill the deck, you get a constraint error later on in the print statement when trying to print a minor arcana. As a note, defaulting the variant portion of the record definition did prevent the constraint error, though obviously the results were wrong because Initialize did not initialize the whole deck= . Try: with Ada.Numerics.Discrete_Random; with Ada.Text_IO; with Ada.Text_IO.Unbounded_IO; with Ada.Strings; with Ada.Strings.Unbounded;=20 procedure Tarot1 is =20 Maximum_Deck_Length: constant integer :=3D 78; subtype Card_Position is Positive range 1..Maximum_Deck_Length; type Card_Suits is (Cups,Pentacles,Swords,Wands); type Trump_Values is new Natural range 0..21; type Card_Values is (Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Page,K= night,Queen,King,Ace); type Card_Info (Trump: Boolean :=3D False) is record -- we will skip giving name and divinatory meaning to cards - its jus= t ordinary assignment but for each card separately; -- PROBLEM: Assignment here does nothing, so I have to assign these v= alues in Initialize procedure below as well Name : Ada.Strings.Unbounded.Unbounded_String :=3D Ada.Strings.Unboun= ded.Null_Unbounded_String; Divinatory_Meaning: Ada.Strings.Unbounded.Unbounded_String :=3D Ada.S= trings.Unbounded.Null_Unbounded_String; case Trump is when True =3D> Trump_Value:Trump_Values; when False =3D> Card_Value:Card_Values; Suit_Value:Card_Suits; end case; end record; type Deck is array (Card_Position) of Card_Info; package Card_Position_Random is new Ada.Numerics.Discrete_Random (Result= _Subtype =3D> Card_Position); procedure Initialize ( TDeck : in out Deck) is Index: Card_Position :=3D 1; begin -- first lay 22 trump cards in a deck for Index_1 in Trump_Values loop TDeck (Index) :=3D (Trump =3D> True, Trump_Value =3D> Index_1, oth= ers =3D> <>); Index :=3D Index + 1; -- Jere: I added this end loop; =20 -- now lay 4 suits of 14 cards each; for Suit_Index in Card_Suits loop for Card_Index in Card_Values loop TDeck (Index) :=3D (Trump =3D> False, Card_Value =3D> Card_Inde= x, Suit_Value =3D> Suit_Index, others =3D> <>); exit when Index=3DMaximum_Deck_Length; -- preventing CONSTRAINT= _ERROR; Index:=3DIndex+1; end loop; end loop; end Initialize; procedure Shuffle (TDeck : in out Deck) is Position_Gen : Card_Position_Random.Generator; Temporary_Data : Card_Info; Index_Random : Card_Position; begin Card_Position_Random.Reset (Gen =3D> Position_Gen); for Index in Card_Position loop Index_Random :=3D Card_Position_Random.Random (Gen =3D> Po= sition_Gen); Temporary_Data :=3D TDeck (Index); TDeck (Index) :=3D TDeck (Index_Random); TDeck (Index_Random) :=3D Temporary_Data; end loop; end Shuffle; Tarot_Deck : Deck;=20 =20 begin =20 Initialize(Tarot_Deck); Shuffle(Tarot_Deck); for Index in Card_Position loop -- just print the deck as shuffled Ada.Text_IO.put("Index =3D " & Card_Position'Image(Index) & " =3D> ")= ; if Tarot_Deck(Index).Trump then Ada.Text_IO.Put_Line("Major Arcana/Trump: "&Trump_Values'Image(Tar= ot_Deck(Index).Trump_Value)); else Ada.Text_IO.Put_Line("Minor Arcana: "&Card_Values'Image(Tarot_Deck= (Index).Card_Value)&" of "&Card_Suits'Image(Tarot_Deck(Index).Suit_Value)); end if; end loop; =20 end Tarot1;