comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: New to Ada, why these warning messages?
Date: Thu, 31 Dec 2015 22:54:29 -0700
Date: 2015-12-31T22:54:29-07:00	[thread overview]
Message-ID: <n6545n$cnt$2@dont-email.me> (raw)
In-Reply-To: <n64nnt$ft6$1@reader1.panix.com>

Botton explained the warnings you got, but a larger issue is that this is poor
Ada and so not the best thing to be learning from.

On 12/31/2015 07:19 PM, Dale Dellutri wrote:
> 
>    -- Some Paint arrays.  The first one is initialized with a list
>    -- of colors.
>    A: AType := (Red, Red, Pink, Blue, Orange, Cherry, Indigo, Indigo);

This is not a list. It's a positional array aggregate.

>    I: Integer;          -- Loop index.

As you've discovered, this is not the loop index. Loop indices are declared by
the loop, are constant within the loop, and cease to exist when the loop is
exited. Whoever wrote this was probably used to languages in which you had to
declare a loop index like this, and hadn't learned how Ada works.

>    -- Use positions to set varioius values in various places.
>    B := (5 => Green, 2 => Orange, 6..8 => Indigo, 1|3|4 => Brown);

This is a named array aggregate.

>    -- Set the entire array to Blue.
>    C := (AType'First .. AType'Last => Blue);

This would be better written

   (Atype'range => Blue)

or

   (Paint => Blue)

>    -- Print the position numbers, spaced out to align with the
>    -- the printouts of each of the arrays below.
>    for I in 1..N loop

I would probably use

   for I in Atype'range loop

to avoid using a "magic number", even in a case like this in which it's unlikely
that a change to the constant won't also result in a change to the array range.

>    -- Print out the contents of each of A, B, and C.
>    for I in 1..N loop

   for I in A'range loop

>       Put(PNames(A(I)) & " ");
> 
>    for I in 1..N loop

   for I in B'range loop

>       Put(PNames(B(I)) & " ");
> 
>    for I in 1..N loop

   for I in C'range loop

>       Put(PNames(C(I)) & " ");

Tying the loop index to the array using 'range makes it easier for the compiler
to optimize away bounds checks.

-- 
Jeff Carter
"Strange women lying in ponds distributing swords
is no basis for a system of government."
Monty Python & the Holy Grail
66

  parent reply	other threads:[~2016-01-01  5:54 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-01  2:19 New to Ada, why these warning messages? Dale Dellutri
2016-01-01  2:47 ` David Botton
2016-01-01  5:54 ` Jeffrey R. Carter [this message]
2016-01-01 21:18   ` Bob Duff
2016-01-01 17:30 ` Dennis Lee Bieber
2016-01-01 20:20 ` Dale Dellutri
2016-01-01 21:24   ` Bob Duff
2016-01-01 21:54     ` Dale Dellutri
2016-01-02 12:45       ` Simon Clubley
2016-01-02 14:21         ` Britt
2016-01-02 15:29           ` Simon Clubley
2016-01-03  7:11         ` J-P. Rosen
2016-01-03  9:32           ` Simon Wright
2016-01-03  9:59           ` Simon Clubley
2016-01-03 14:23             ` David Botton
2016-01-03 15:48             ` J-P. Rosen
2016-01-03 18:03               ` David Botton
2016-01-03 18:20                 ` Pascal Obry
2016-01-03 19:05                   ` David Botton
2016-01-01 23:50   ` Jeffrey R. Carter
2016-01-02 21:05     ` Bob Duff
2016-01-02 22:53       ` Jeffrey R. Carter
2016-01-02 17:59   ` Dennis Lee Bieber
2016-01-02 18:37     ` Paul Rubin
2016-01-02 21:03       ` Bob Duff
2016-01-02 21:08     ` Bob Duff
replies disabled

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