<div dir="ltr"><div dir="ltr"><div dir="ltr"><div>On Sat, Nov 3, 2018 at 8:01 AM Schindler Karl-Michael <<a href="mailto:karl-michael.schindler@web.de">karl-michael.schindler@web.de</a>> wrote:<br></div></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi<br>
<br>
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:<br>
<br>
myArray := 5;<br>
<br>
With arrays with defined bounds, it works, but i was not able to do it with a dynamic array. My test case is this:<br>
<br>
program arrayAssign;<br>
<br>
type<br>
TmyArray = array of integer;<br>
<br>
var<br>
myArray: TmyArray;<br>
index: integer;<br>
<br>
operator := (const number: integer) theResult: TmyArray;<br>
var<br>
i: integer;<br>
begin<br>
for i := low(theResult) to high(theResult) do<br>
theResult[i] := number;<br>
end;<br>
<br>
begin<br>
setlength(myArray, 10);<br>
writeln ('myArray: ', low(myArray), ', ', high(myArray));<br>
<br>
myArray := 5;<br>
<br>
writeln ('myArray: ', low(myArray), ', ', high(myArray));<br>
for index := low(myArray) to high(myArray) do<br>
writeln (index, ': ', myArray[index]);<br>
end.<br>
<br>
The output is:<br>
<br>
myArray: 0, 9<br>
myArray: 0, -1<br>
<br>
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?<br>
<br>
Michael, aka MiSchi.<br>
_______________________________________________<br>
fpc-devel maillist - <a href="mailto:fpc-devel@lists.freepascal.org" target="_blank">fpc-devel@lists.freepascal.org</a><br>
<a href="http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel" rel="noreferrer" target="_blank">http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel</a></blockquote><div><br></div>There's two issues:<div>1) The result of the operator is always a new, unique array (as in it's unrelated to the myArray variable)</div><div>2) Your operator doesn't initialize the result with a length, so it's just iterating over nothing.</div><div><br></div><div>Here's an example of the correct way to write the operator and use it:</div><div><br></div><div><div>program ArrayOverloads;</div><div><br></div><div>{$mode ObjFPC}</div><div><br></div><div>type TIntArray = array of Integer;</div><div><br></div><div> operator := (const I: Integer): TIntArray; inline;</div><div> var V: Integer;</div><div> begin</div><div> SetLength(Result, I);</div><div> for V := 0 to Pred(I) do Result[V] := V;</div><div> end;</div><div><br></div><div>var</div><div> I: Integer;</div><div> IA: TIntArray;</div><div><br></div><div>begin</div><div> IA := 25;</div><div> for I in IA do Write(I, ' ');</div><div> WriteLn;</div><div> for I := High(IA) downto 0 do Write(I, ' ');</div><div> WriteLn;</div><div> for I := Low(IA) to High(IA) do Write(I, ' ');</div><div> WriteLn;</div><div> I := 0;</div><div> while I < Pred(High(IA)) do begin</div><div> Inc(I, 2);</div><div> Write(I, ' ');</div><div> end;</div><div>end.</div></div><div><br></div><div>Output:</div><div><br></div><div><div>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</div><div>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</div><div>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</div></div><div>2 4 6 8 10 12 14 16 18 20 22 24 <br></div></div></div>