Recent

Author Topic: [SOLVED] Dynamic array - when to use DArray += [value];  (Read 854 times)

egsuh

  • Hero Member
  • *****
  • Posts: 682
[SOLVED] Dynamic array - when to use DArray += [value];
« 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.    
« Last Edit: January 14, 2021, 04:05:03 am by egsuh »

lucamar

  • Hero Member
  • *****
  • Posts: 3766
Re: Dynamic array - when to use DArray += [value];
« Reply #1 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 ...
« Last Edit: January 13, 2021, 05:47:20 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2886
  • Compiler Developer
Re: Dynamic array - when to use DArray += [value];
« Reply #2 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
  • add {$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

  • Hero Member
  • *****
  • Posts: 3766
Re: Dynamic array - when to use DArray += [value];
« Reply #3 on: January 13, 2021, 03:35:17 pm »
  • make sure that you're using 3.2.0 or newer
  • add {$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 :-X)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

egsuh

  • Hero Member
  • *****
  • Posts: 682
Re: Dynamic array - when to use DArray += [value];
« Reply #4 on: January 14, 2021, 04:04:45 am »
Quote
add {$modeswitch arrayoperators}

Oh yes...  In mode Delphi, I didn't need this.  Thank you.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2886
  • Compiler Developer
Re: Dynamic array - when to use DArray += [value];
« Reply #5 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. ;)

lucamar

  • Hero Member
  • *****
  • Posts: 3766
Re: Dynamic array - when to use DArray += [value];
« Reply #6 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 :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2886
  • Compiler Developer
Re: [SOLVED] Dynamic array - when to use DArray += [value];
« Reply #7 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.

 

TinyPortal © 2005-2018