comp.lang.ada
 help / color / mirror / Atom feed
From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Generic Zero Length Array
Date: Fri, 22 Feb 2008 09:26:38 -0500
Date: 2008-02-22T09:26:38-05:00	[thread overview]
Message-ID: <wccoda912zl.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: 9b771018-fb0b-42eb-ae00-12ee3eda69b2@p43g2000hsc.googlegroups.com

shaunpatterson@gmail.com writes:

> I have a bit of legacy code in my system that I am trying to remove
> all warnings from.
>
> The package is a generic -- with:
>
> type Element is (<>);
> type Element_List is array (Indexing range <>) of Element;
>
>
> I have been stumped by one compiler warning where one of the functions
> needs to return a zero-length empty generic array:
>
> warning: variable Result_String is read but never assigned

If the warning is wrong, the appropriate thing to do is
pragma Warnings(Off, ...).  See the docs for various useful
options.  Also look at pragma Unreferenced, which might be
appropriate in other cases.

> code:
>
> if Error_Condition then
>   declare
>     type Result is new Element_List (Indexing.First + 1 ..
> Indexing'First);
>     Result_String : Result;
>   begin
>      return Element_List (Result);
>   end;
> end if;

You've got some typos there -- better to cut&paste the exact code.

I can't tell for sure without more context, but that code looks strange.
Why declare a new type?  Why not:

    subtype Result is Element_List (...);
    Result_String : Result;
    ...
    return Result_String;

Or:

    Result_String : Element_List (...);
    ...
    return Result_String;

Or (in Ada 2005):

    return Element_List'(Indexing'Base'First+1 .. Indexing'Base'First => <>);

And it wouldn't hurt to put:

    pragma Assert (Indexing'Base'First < Indexing'Base'Last);

in the package spec, since this generic won't work with
a one-element or zero-element base type.

> How do I initialize this array when I can't know what type it is or
> will be?

You don't have to initialize it.  But if you want to, an aggregate will
work.

- Bob



  parent reply	other threads:[~2008-02-22 14:26 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-02-22 13:31 Generic Zero Length Array shaunpatterson
2008-02-22 13:53 ` Egil Høvik
2008-02-22 13:55   ` shaunpatterson
2008-02-22 14:11     ` Georg Bauhaus
2008-02-22 19:25   ` Randy Brukardt
2008-02-22 23:01     ` Adam Beneschan
2008-02-23  9:30       ` Dmitry A. Kazakov
2008-02-23 14:27       ` Robert A Duff
2008-02-23 16:16         ` Dmitry A. Kazakov
2008-02-25 16:41         ` Adam Beneschan
2008-02-25 19:14           ` Robert A Duff
2008-02-22 19:25   ` Randy Brukardt
2008-02-22 19:25   ` Randy Brukardt
2008-02-22 14:23 ` Stefan Lucks
2008-02-22 16:52   ` Adam Beneschan
2008-02-22 14:26 ` Robert A Duff [this message]
2008-02-22 15:22 ` Stefan Bellon
2008-02-22 23:03   ` Adam Beneschan
2008-02-23 10:19     ` Stefan Bellon
replies disabled

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