[SOLVED] Dynamic array - when to use DArray += [value]; lazarus / 21-General ---------------------------------------------------------------------- egsuh · 2021-01-13 04:09 Hi, What can I do in the following case? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var    EList: array of integer;    i: integer; begin    // ......    EList += [i];   // Compiler says "Dynamic Array of LongInt" + "Set of Byte" is not overloaded. end; ---------------------------------------------------------------------- lucamar · 2021-01-13 04:44 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var    EList: array of integer;    i, Last: integer;begin    // ......    Last := Length(EList);    SetLength(EList, Last+1)    EList[Last] := i;end; If you really wish I guess you could overload the + operator to do that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i { COMPLETELY UNTESTED!!!  Might need some (re)work ...}type  TIntArray = array of Integer;  operator + (a: TIntArray; i: integer): TIntArray;begin  Result := a;  SetLength(Result, Length(a)+1);  Result[High(Result)] := i;end; {...}{ now you should be able to do ...}var    EList: array of integer;    i: integer;begin    // ......  EList += i;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 ... ---------------------------------------------------------------------- PascalDragon · 2021-01-13 07:47 Quote from: egsuh on January 13, 2021, 05:09:59 amWhat can I do in the following case? make sure that you're using 3.2.0 or neweradd {$modeswitch arrayoperators} 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-}). ---------------------------------------------------------------------- lucamar · 2021-01-13 14:35 Quote from: PascalDragon on January 13, 2021, 08:47:48 ammake sure that you're using 3.2.0 or neweradd {$modeswitch arrayoperators} 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 ) ---------------------------------------------------------------------- egsuh · 2021-01-14 03:04 Quoteadd {$modeswitch arrayoperators} Oh yes...  In mode Delphi, I didn't need this.  Thank you. ---------------------------------------------------------------------- PascalDragon · 2021-01-14 08:06 Quote from: lucamar on January 13, 2021, 03:35:17 pmPlease, disregard my previous post (though I still think that sintax is ugly ) Why? Arrays have been used like this inside code for decades with open array parameters. If you mean the +=, then I agree however. ---------------------------------------------------------------------- lucamar · 2021-01-14 09:50 Quote from: PascalDragon on January 14, 2021, 09:06:31 amIf 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 ---------------------------------------------------------------------- PascalDragon · 2021-01-14 14:12 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.