comp.lang.ada
 help / color / mirror / Atom feed
From: bobduff@dsd.camb.inmet.com (Bob Duff)
Subject: Re: Ada can't initialize (aggregate) array of length 1?
Date: Mon, 12 Dec 1994 18:35:28 GMT
Date: 1994-12-12T18:35:28+00:00	[thread overview]
Message-ID: <D0pnn4.9Cx@inmet.camb.inmet.com> (raw)
In-Reply-To: 3chh6i$lp7@cnj.digex.net

In article <3chh6i$lp7@cnj.digex.net>, Gentle <gentle@cnj.digex.net> wrote:
>On Mon, 12 Dec 94 08:12:30 CET, Dirk Zoller (duz@roxi.rz.fht-mannheim.de) wrote:
>: Simplified I did this:
>
>:     type stone is array (1..2) of character;
>:     shapes: constant array (1..1) of stone := ("[]");
>:     ____________________________________________^

Yes, this is an annoyance.  You can't write a one-element positional
aggregate in Ada.  Nor a zero-element aggregate.

To use a one-element aggregate, you have to use named notation, as in:
    Shapes: constant array(1..1) of Stone := (1 => "[]");

Or:
    Shapes: constant array(1..1) of Stone := (others => "[]");

>  You're trying to squash a string of length 2 into a character space.

No, stone is a string of length two, and the string literal is of length
two, so that part of it is OK.  The only thing wrong with the original
example is that it tries to use positional notation for a one-element
aggregate, which is not allowed -- named notation is required.

>Try initializing like this:
>
>  shapes: constant array (1..1) of stone := (1 => ('[',']'));
                                                   ^^^^^^^^^ This will
work, but you can use the string literal there instead, if you want.

>Although type stone could have been declared as a subtype of STRING of
>length 2, then your initialization would have worked.

String literals work for all string types.  A string type is any
one-dimensional array type whose element type is a character type.
You don't have to use the predefined type String in order to use string
literals.

The reason for the aggregate restriction is that it would be difficult
for compilers to determine whether:

    ( exp )

is a parenthesized expression of some type, or an aggregate of an array
type.  If Ada had used some other notation for aggregates (say, [...]),
then this problem would not exist.  However, I believe the original
requirements for Ada forbade using certain Ascii characters, like [ and
], because those characters were not available on all hardware and
certain characters are used for different purposes in certain countries
that have addition letters not in Ascii.

It's still an annoyance not to have a clean notation for zero and one
element aggregates.

- Bob
-- 
Bob Duff                                bobduff@inmet.com
Oak Tree Software, Inc.
Ada 9X Mapping/Revision Team (Intermetrics, Inc.)



  reply	other threads:[~1994-12-12 18:35 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-12-12  7:12 Ada can't initialize (aggregate) array of length 1? Dirk Zoller
1994-12-12 12:54 ` Gentle
1994-12-12 18:35   ` Bob Duff [this message]
1994-12-13 17:01   ` Jahn Rentmeister
1994-12-14  3:23   ` Robert Dewar
1994-12-12 13:20 ` R. William Beckwith
1994-12-16 13:34   ` gamache
1994-12-14  3:21 ` Robert Dewar
1994-12-16 10:37   ` Dirk Zoller
1994-12-19 14:20     ` Robert Dewar
1994-12-16 16:41   ` Scott Leschke
1994-12-17 17:52     ` Robert Dewar
replies disabled

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