Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Language/Dynamic Arrays    [ Add a report in this area ]  
Report #:  4839   Status: Reported
Improper handling of const dynamic arrays
Project:  Delphi Build #:  4.453
Version:    7.0 Submitted By:   Ignacio Vazquez
Report Type:  Minor failure / Design problem Date Reported:  6/25/2003 10:12:30 AM
Severity:    Infrequently encountered problem Last Updated: 8/29/2003 2:27:22 PM
Platform:    All versions Internal Tracking #:  
Resolution: None  Resolved in Build: : None
Duplicate of:  None
Voting and Rating
Overall Rating: (6 Total Ratings)
3.33 out of 5
Total Votes: None
Description
Observe:

const
  MyConstArray: array of Integer=
  (0, 1, 2, 3);

The above code fails with the compiler claiming that there should be a ')' where the first ',' is. When the code is changed to the following:

const
  MyConstArray: array of Integer=
  (0);

the compiler claims that the 0 was converted to nil.

This indicates that the dynamic array is trying to use the value assigned as a pointer instead of an array as would be desired.
Steps to Reproduce:
Enter the following:

const
  MyConstArray: array of Integer=
  (0, 1, 2, 3);

This will fail to compile.

Enter the following:

const
  MyConstArray:array of Integer=
  (0);

This will compile, but not with the desired result.
Workarounds
Use a static array instead:

const
  MyConstArray: array [0..3] of Integer=
  (0, 1, 2, 3);
Attachment
None
Comments

Will DeWitt Jr. at 8/28/2003 2:22:53 AM -
If I understand the report correctly, what you want is an pre-initialized dynamic array.  The workaround isn't appropriate to this because that array cannot be further modified using SetLength (or so I believe, anyways).  You might want to modify the report to make it clearer what the perceived problem is, and how you believe it can be fixed (or rather, how it should work).  Also, modify your "Steps" code (and the code in your Description) to note that you must include the "Types" unit in order to use TIntegerDynArray.  And if I'm misintepreting what you're saying / wanting, please let me know.  =)

A correct workaround would be this--

uses
  Types;

const
  MyDefaultDynArrayData: array[0..3] of integer =
    (0, 1, 2, 3);

  MyConstArray: TIntegerDynArray =
    @MyDefaultDynArrayData;

----------

It would be nice to sidestep the issue of creating a seperate const array and instead have the compiler generate a hidden / unnamed array then reference that automagically, but clearly the compiler is expecting a pointer for an initialized dynamic array.

The compiler, for TIntegerDynArray, is expecting a pointer.  You can also initialize it to 'nil' (hence why it was taking 0 to mean a nil pointer).

Ignacio Vazquez at 8/28/2003 6:41:34 AM -
>If I understand the report correctly, what you want is an pre-initialized dynamic array.

What I want is a const array that I don't have to specify the size for. I figure that since the compiler should be smart enough to be able to count the number of items I'm passing to it, that it should be able to convert it to a static array with the appropriate number of elements when compiling.

Obviously the ideas of "const" and "dynamic" are in conflict, so there would be little usefulness in having an actual const dynamic array.

Daniel England at 11/28/2006 7:58:05 PM -
Hmm...  I get this quite a bit.  I personally resort to the example given in the comments, to define an array "sized by hand" and then specify the address to another value.

The problem comes with the means by which you define a dynamic array conflicting with the way in which you declare initialised arrays.

Personally, I don't think dynamic array declarations should be permitted as const.  But, implementing that could break existing programs...

Instead, my logical expectation is to use another const specifier...  Something like:

    const
    ARR_VAL_BLAH: const array of Integer = (0, 1, 2);


Of course, you don't want to use SetLength on it at run-time and this is fairly clearly stating the terms.

*sigh*  Bascially, its just a "nice to have" feature since its easy enough to count the members and declare it for yourself.  However, having to "work around" the fact that the compiler doesn't like mixing them sometimes, is nasty.  This could resolve the issue.


Daniel.

Server Response from: ETNACODE01