comp.lang.ada
 help / color / mirror / Atom feed
* Advent of Code Day 7
@ 2020-12-07 23:03 John Perry
  2020-12-07 23:44 ` Randy Brukardt
  2020-12-08 11:25 ` Jeffrey R. Carter
  0 siblings, 2 replies; 6+ messages in thread
From: John Perry @ 2020-12-07 23:03 UTC (permalink / raw)


When doing today's exercise, I encountered an issue where I wanted to initialize only some fields of a record, with the other fields having a default initialization. In particular:

   type Bag_Entry is record
      Description: Bag_Description := "                    ";
      Quantity: Positive;
   end record;

  Entry: Bag_Entry := ( Quantity => 10 );

However, GNAT says this is invalid because I didn't specify Bag_Description. I looked in the ARM but didn't see anyplace that implies that; did I miss it? Is there a reason that Description can't be initialized automatically according to the rules above?

john perry

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

* Re: Advent of Code Day 7
  2020-12-07 23:03 Advent of Code Day 7 John Perry
@ 2020-12-07 23:44 ` Randy Brukardt
  2020-12-07 23:49   ` John Perry
  2020-12-08 11:25 ` Jeffrey R. Carter
  1 sibling, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2020-12-07 23:44 UTC (permalink / raw)


"John Perry" <john.perry@usm.edu> wrote in message 
news:df3d282a-ff8c-4fd7-befc-a6ca683215e1n@googlegroups.com...
> When doing today's exercise, I encountered an issue where I wanted to 
> initialize only some fields of a record, with the other fields having a 
> default initialization. In particular:
>
>   type Bag_Entry is record
>      Description: Bag_Description := "                    ";
>      Quantity: Positive;
>   end record;
>
>  Entry: Bag_Entry := ( Quantity => 10 );
>
> However, GNAT says this is invalid because I didn't specify 
> Bag_Description. I looked in the ARM but didn't see anyplace that implies 
> that; did I miss it? Is there a reason that Description can't be 
> initialized automatically according to the rules above?

In Ada 2005 and later, write:

   Entry: Bag_Entry := (Quantity => 10, Description => <>);

In an aggregate, <> means a default initialized component. Following the Ada 
Way TM ;-), one has to explicitly ask for a default initialized component --  
just leaving it out might have been a mistake or intended -- neither the 
compiler nor a reader can tell. The above is clearly intended.

                    Randy.


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

* Re: Advent of Code Day 7
  2020-12-07 23:44 ` Randy Brukardt
@ 2020-12-07 23:49   ` John Perry
  0 siblings, 0 replies; 6+ messages in thread
From: John Perry @ 2020-12-07 23:49 UTC (permalink / raw)


On Monday, December 7, 2020 at 5:44:48 PM UTC-6, Randy Brukardt wrote:
> Entry: Bag_Entry := (Quantity => 10, Description => <>); 
> 
> In an aggregate, <> means a default initialized component. Following the Ada 
> Way TM ;-), one has to explicitly ask for a default initialized component -- 
> just leaving it out might have been a mistake or intended -- neither the 
> compiler nor a reader can tell. The above is clearly intended. 

That makes sense and works! Thank you!

john perry

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

* Re: Advent of Code Day 7
  2020-12-07 23:03 Advent of Code Day 7 John Perry
  2020-12-07 23:44 ` Randy Brukardt
@ 2020-12-08 11:25 ` Jeffrey R. Carter
  2020-12-08 16:55   ` John Perry
  1 sibling, 1 reply; 6+ messages in thread
From: Jeffrey R. Carter @ 2020-12-08 11:25 UTC (permalink / raw)


On 12/8/20 12:03 AM, John Perry wrote:
> 
>     type Bag_Entry is record
>        Description: Bag_Description := "                    ";

Humans are notoriously bad at counting things, and even worse at counting things 
they can't see, so this kind of literal can be a source of errors, especially 
during modification. (At least with Ada these tend to be compiler errors, not 
run-time errors.)

Of course, Ada offers a Better Way. You can write

    Description: Bag_Description := (Bag_Description'range => ' ');

or

    Description: Bag_Description := (others => ' ');

and be proof against any changes to Bag_Description's bounds.

I had a similar record, but I used Unbounded_String.

-- 
Jeff Carter
"Violence is the last refuge of the incompetent."
Foundation
151

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

* Re: Advent of Code Day 7
  2020-12-08 11:25 ` Jeffrey R. Carter
@ 2020-12-08 16:55   ` John Perry
  2020-12-08 23:25     ` Jeffrey R. Carter
  0 siblings, 1 reply; 6+ messages in thread
From: John Perry @ 2020-12-08 16:55 UTC (permalink / raw)


On Tuesday, December 8, 2020 at 5:25:57 AM UTC-6, Jeffrey R. Carter wrote:
> Humans are notoriously bad at counting things, and even worse at counting things 
> they can't see, so this kind of literal can be a source of errors, especially 
> during modification. (At least with Ada these tend to be compiler errors, not 
> run-time errors.) 
> 
> Of course, Ada offers a Better Way. You can write 
> 
> Description: Bag_Description := (Bag_Description'range => ' '); 
> 
> or 
> 
> Description: Bag_Description := (others => ' '); 
> 
> and be proof against any changes to Bag_Description's bounds. 

Very much appreciated! I was aware of "others =>" for arrays and records but didn't think to try it with strings. "Bag_Description'range =>" is entirely new to me.

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

* Re: Advent of Code Day 7
  2020-12-08 16:55   ` John Perry
@ 2020-12-08 23:25     ` Jeffrey R. Carter
  0 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2020-12-08 23:25 UTC (permalink / raw)


> 
> Very much appreciated! I was aware of "others =>" for arrays and records but didn't think to try it with strings. "Bag_Description'range =>" is entirely new to me.

A string type is just a 1D array type with a character type for components. The 
definition of type String in pkg Standard is given in ARM A.1 as

type String is array (Positive range <>) of Character with Pack;

(http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-A-1.html)

A string literal is a special kind of array aggregate for string types, but 
normal array aggregates also work.

Bag_Description is clearly a constrained string subtype, and S'range is defined 
for all constrained array subtypes.

-- 
Jeff Carter
"Violence is the last refuge of the incompetent."
Foundation
151

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

end of thread, other threads:[~2020-12-08 23:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-07 23:03 Advent of Code Day 7 John Perry
2020-12-07 23:44 ` Randy Brukardt
2020-12-07 23:49   ` John Perry
2020-12-08 11:25 ` Jeffrey R. Carter
2020-12-08 16:55   ` John Perry
2020-12-08 23:25     ` Jeffrey R. Carter

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