Hi,

Leaving aside the reason why the MiSchi's solution doesn't work the main question is still not answered :)

If you have integer dynamic array "MyArray" is there a way for the following statement to compile and work correctly:

MyArray := 5;

Thanks,
Gennady


On 11/3/2018 4:14 PM, Ben Grasset wrote:
On Sat, Nov 3, 2018 at 8:01 AM Schindler Karl-Michael <karl-michael.schind...@web.de <mailto:karl-michael.schind...@web.de>> wrote:

    Hi

    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;

    type
      TmyArray = array of integer;

    var
      myArray: TmyArray;
      index: integer;

    operator := (const number: integer) theResult: TmyArray;
      var
        i: integer;
      begin
        for i := low(theResult) to high(theResult) do
          theResult[i] := number;
      end;

    begin
      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]);
    end.

    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.
    _______________________________________________
    fpc-devel maillist  - fpc-devel@lists.freepascal.org
    <mailto:fpc-devel@lists.freepascal.org>
    http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


There's two issues:
1) The result of the operator is always a new, unique array (as in it's unrelated to the myArray variable) 2) Your operator doesn't initialize the result with a length, so it's just iterating over nothing.

Here's an example of the correct way to write the operator and use it:

program ArrayOverloads;

{$mode ObjFPC}

type TIntArray = array of Integer;

  operator := (const I: Integer): TIntArray; inline;
  var V: Integer;
  begin
    SetLength(Result, I);
    for V := 0 to Pred(I) do Result[V] := V;
  end;

var
  I: Integer;
  IA: TIntArray;

begin
  IA := 25;
  for I in IA do Write(I, ' ');
  WriteLn;
  for I := High(IA) downto 0 do Write(I, ' ');
  WriteLn;
  for I := Low(IA) to High(IA) do Write(I, ' ');
  WriteLn;
  I := 0;
  while I < Pred(High(IA)) do begin
    Inc(I, 2);
    Write(I, ' ');
  end;
end.

Output:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
2 4 6 8 10 12 14 16 18 20 22 24


_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to