# Lazarus

## Programming => General => Topic started by: egsuh on January 13, 2021, 05:09:59 am

Title: [SOLVED] Dynamic array - when to use DArray += [value];
Post by: egsuh on January 13, 2021, 05:09:59 am
Hi,

What can I do in the following case?

Code: Pascal  [Select][+][-]
1. var
2.     EList: array of integer;
3.     i: integer;
4. begin
5.     // ......
6.     EList += [i];   // Compiler says "Dynamic Array of LongInt" + "Set of Byte" is not overloaded.
7. end;
8.
Title: Re: Dynamic array - when to use DArray += [value];
Post by: lucamar on January 13, 2021, 05:44:12 am
The compiler, logically enough, thinks that you're trying to add a dynamic array and a set (as deduced from the construct: [i]) and she doesn't know how to do that.

What you should write instead depends on what you want to do with such exotic sintax: if, as seems logical, you want to append the integer as the last element of the array, do something like:

Code: Pascal  [Select][+][-]
1. var
2.     EList: array of integer;
3.     i, Last: integer;
4. begin
5.     // ......
6.     Last := Length(EList);
7.     SetLength(EList, Last+1)
8.     EList[Last] := i;
9. end;

If you really wish I guess you could overload the + operator to do that:
Code: Pascal  [Select][+][-]
1. { COMPLETELY UNTESTED!!!
2.   Might need some (re)work ...}
3. type
4.   TIntArray = array of Integer;
5.
6. operator + (a: TIntArray; i: integer): TIntArray;
7. begin
8.   Result := a;
9.   SetLength(Result, Length(a)+1);
10.   Result[High(Result)] := i;
11. end;
12.
13. {...}
14. { now you should be able to do ...}
15. var
16.     EList: array of integer;
17.     i: integer;
18. begin
19.     // ......
20.     EList += i;
21. end;

though I wouldn't recommend it. IMHO, it looks really ... outrĂª ;)

If what you want to do is some other thing, please explain with a little more detail ...
Title: Re: Dynamic array - when to use DArray += [value];
Post by: PascalDragon on January 13, 2021, 08:47:48 am
What can I do in the following case?

• make sure that you're using 3.2.0 or newer

As the builtin +-operator for arrays disables custom operator overloads the modeswitch is disabled by default except in mode Delphi (where it can be disabled with {\$modeswitch arrayoperators-}).
Title: Re: Dynamic array - when to use DArray += [value];
Post by: lucamar on January 13, 2021, 03:35:17 pm
• make sure that you're using 3.2.0 or newer

Oops! I forgot again the "new" creator for dynamic arrays, which would make the compiler interpret "[i]" as a new array and allow it to be "added" to other dyn. arrays ... :-[

Please, disregard my previous post (though I still think that sintax is ugly :-X)
Title: Re: Dynamic array - when to use DArray += [value];
Post by: egsuh on January 14, 2021, 04:04:45 am
Quote

Oh yes...  In mode Delphi, I didn't need this.  Thank you.
Title: Re: Dynamic array - when to use DArray += [value];
Post by: PascalDragon on January 14, 2021, 09:06:31 am
Please, disregard my previous post (though I still think that sintax is ugly :-X)

Why? Arrays have been used like this inside code for decades with open array parameters. :o

If you mean the +=, then I agree however. ;)
Title: Re: Dynamic array - when to use DArray += [value];
Post by: lucamar on January 14, 2021, 10:50:28 am
If you mean the +=, then I agree however. ;)

I meant the combination of both: the "+=" makes the single-element array, alone there by itself, look confusing and out of place; it's not inmediately discernible what that means.

Of course, that might be just I looking at a little (or never, in my case) used construction and other people find it quite normal :-[
Title: Re: [SOLVED] Dynamic array - when to use DArray += [value];
Post by: PascalDragon on January 14, 2021, 03:12:24 pm
In (Object) Pascal, if you have [ ... ] on the right side as a separate element (not a property index), it's either a set or an array. And += adds that set or array to the left hand side. Don't know what should be confusing or out of place here.