[fpc-devel] Array assignment operator

Schindler Karl-Michael karl-michael.schindler at web.de
Sat Nov 3 12:56:29 CET 2018


I would like to use a simple assignment operator for arrays, with which all elements of an array are assigned to the same value, similar to an extended initialize, not to zero but to a value of choice. A simple example for an integer array would be:

  myArray := 5;

With arrays with defined bounds, it works, but i was not able to do it with a dynamic array. My test case is this:

program arrayAssign;

  TmyArray = array of integer;

  myArray: TmyArray;
  index: integer;
operator := (const number: integer) theResult: TmyArray;
    i: integer;
    for i := low(theResult) to high(theResult) do
      theResult[i] := number;

  setlength(myArray, 10);
  writeln ('myArray: ', low(myArray), ', ', high(myArray));

  myArray := 5;

  writeln ('myArray: ', low(myArray), ', ', high(myArray));
  for index := low(myArray) to high(myArray) do
    writeln (index, ': ', myArray[index]);

The output is:

myArray: 0, 9
myArray: 0, -1

The problem is that in the declaration of the operator, the functions low and high seem to return the values of the type TmyArray, i.e. 0 and -1 and not the values of the variable of the assignment statement, i.e. myArray and the assignment nullifies the previous setlength. Is there any way around this and obtain the actual values of myArray?

Michael, aka MiSchi.

More information about the fpc-devel mailing list