Recent

Author Topic: Arrays as parameters  (Read 1798 times)

jollytall

  • New Member
  • *
  • Posts: 46
Re: Arrays as parameters
« Reply #15 on: February 18, 2020, 10:50:29 am »
PascalDragon,
Thanks a lot for your very detailed and complete answer. It really explains a lot. It should almost be part of the documentation it is so clear.

For 3.2.0 and newer you can use the normal array syntax (which also works for assignments):
It sounds exciting. If I see well, it is not released yet, but already this information helps me to choose which way to go. I will go with the TDynamicArray logic, and temporarily call it with the constructor, hoping that soon it can be called without.
Any expected publishing date of 3.2?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8884
  • FPC developer.
Re: Arrays as parameters
« Reply #16 on: February 18, 2020, 11:00:06 am »
Note that open arrays are D1 and Dynamic arrays only D4. Open Arrays were first.

(and the "string" as parameter type in Turbo Pascal was basically an open array type specifically for strings. It is called "openstring" under Delphi)

uart

  • New Member
  • *
  • Posts: 40
Re: Arrays as parameters
« Reply #17 on: February 19, 2020, 08:23:28 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   arr: array of LongInt;
  3. begin
  4.   arr := [1, 2, 3, 4];
  5. end.

I don't have fpc ver 3.20 to test it, but that is interesting. I like the idea of being able to assign multiple array values at once. Very useful for short arrays.

So does that line: "arr:=[1,2,3,4];" do the equivalent of a setLength(arr,4) and then initialize the array with the data values?

Just wondering what happens if you use that construct to not only initalize arr, but also to assign new values to it during run time. Say for example the following. Would the second arr:=[...] construct create a new array, or would it just modify the values in the existing array?

Code: Pascal  [Select][+][-]
  1. var arr: array of LongInt;
  2. begin
  3.   arr := [1, 2, 3, 4];
  4.  // .... do something with arr
  5.   arr := [5, 6, 7, 8];
  6.  // ... do something else with arr
  7. end.

PascalDragon

  • Hero Member
  • *****
  • Posts: 2395
  • Compiler Developer
Re: Arrays as parameters
« Reply #18 on: February 19, 2020, 09:25:24 am »
It sounds exciting. If I see well, it is not released yet, but already this information helps me to choose which way to go. I will go with the TDynamicArray logic, and temporarily call it with the constructor, hoping that soon it can be called without.

Please note that the Create variant will still be available in 3.2 and beyond, so even if you use it now you don't need to change it later on.

Any expected publishing date of 3.2?

Soon (TM).

Code: Pascal  [Select][+][-]
  1. var
  2.   arr: array of LongInt;
  3. begin
  4.   arr := [1, 2, 3, 4];
  5. end.

I don't have fpc ver 3.20 to test it, but that is interesting. I like the idea of being able to assign multiple array values at once. Very useful for short arrays.

So does that line: "arr:=[1,2,3,4];" do the equivalent of a setLength(arr,4) and then initialize the array with the data values?

Just wondering what happens if you use that construct to not only initalize arr, but also to assign new values to it during run time. Say for example the following. Would the second arr:=[...] construct create a new array, or would it just modify the values in the existing array?

Code: Pascal  [Select][+][-]
  1. var arr: array of LongInt;
  2. begin
  3.   arr := [1, 2, 3, 4];
  4.  // .... do something with arr
  5.   arr := [5, 6, 7, 8];
  6.  // ... do something else with arr
  7. end.


An inline array constructor is the equivalent of the following:

Code: Pascal  [Select][+][-]
  1. var
  2.   arr, tmp: array of LongInt;
  3. begin
  4.   // arr := [1, 2, 3, 4];
  5.   SetLength(tmp, 4);
  6.   tmp[0] := 1;
  7.   tmp[1] := 2;
  8.   tmp[2] := 3;
  9.   tmp[3] := 4;
  10.   arr := tmp;
  11.   tmp := Nil;
  12.   // ...
  13.   // arr := [5, 6, 7, 8];
  14.   SetLength(tmp, 4);
  15.   tmp[0] := 5;
  16.   tmp[1] := 6;
  17.   tmp[2] := 7;
  18.   tmp[3] := 8;
  19.   arr := tmp;
  20.   tmp := Nil;
  21. end;

There might be optimizations for this in the future (e.g. creating a constant array and assigning that if all values are constant), but in general this is how this works.

Thaddy

  • Hero Member
  • *****
  • Posts: 10559
Re: Arrays as parameters
« Reply #19 on: February 19, 2020, 10:17:23 am »
You can even do this in 3.2.0+
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. var
  3.   arr: array of LongInt = [1, 2, 3, 4]; // initialize at declare time
  4. begin
  5. end.
As per my demo.
Personally I find this the most powerful feature regarding array syntax or any other declarations.
« Last Edit: February 19, 2020, 10:20:12 am by Thaddy »

uart

  • New Member
  • *
  • Posts: 40
Re: Arrays as parameters
« Reply #20 on: February 19, 2020, 10:31:22 am »
An inline array constructor is the equivalent of the following:

Code: Pascal  [Select][+][-]
  1. var
  2.   arr, tmp: array of LongInt;
  3. begin
  4.   // arr := [1, 2, 3, 4];
  5.   SetLength(tmp, 4);
  6.   tmp[0] := 1;
  7.   tmp[1] := 2;
  8.   tmp[2] := 3;
  9.   tmp[3] := 4;
  10.   arr := tmp;
  11.   tmp := Nil;
  12.   // ...
  13.   // arr := [5, 6, 7, 8];
  14.   SetLength(tmp, 4);
  15.   tmp[0] := 5;
  16.   tmp[1] := 6;
  17.   tmp[2] := 7;
  18.   tmp[3] := 8;
  19.   arr := tmp;
  20.   tmp := Nil;
  21. end;

Ok thanks. So does that mean that this construct will not work to assign multiple values to normal static arrays? Because I'd love to be able to do that.

Say this code for example. Can this (or something similar) be done with a static array?

Code: Pascal  [Select][+][-]
  1. var  p[1..5] : array of Integer;
  2. begin
  3. p := [2,3,5,7,11]
  4. end;

Thaddy

  • Hero Member
  • *****
  • Posts: 10559
Re: Arrays as parameters
« Reply #21 on: February 19, 2020, 10:44:27 am »
Well, slightly different syntax as my previous:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}// or {$objfpc}
  2. var
  3.   arr: array[0..3] of LongInt = (1, 2, 3, 4);
  4. begin
  5. end.
Note brackets, so yes.Same syntax for objfpc mode.But your example is not supported afaik.
« Last Edit: February 19, 2020, 10:56:33 am by Thaddy »

uart

  • New Member
  • *
  • Posts: 40
Re: Arrays as parameters
« Reply #22 on: February 19, 2020, 11:17:11 am »
Well, slightly different syntax as my previous:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}// or {$objfpc}
  2. var
  3.   arr: array[0..3] of LongInt = (1, 2, 3, 4);
  4. begin
  5. end.
Note brackets, so yes.Same syntax for objfpc mode.But your example is not supported afaik.

Sorry that's not what I meant. I know it can be done during variable declaration, but it has always frustrated me that a similar construct is not available after declaration. I'm interested to know if there's something more like the following available for static arrays.

Code: Pascal  [Select][+][-]
  1. var  p[1..5] : array of Integer;
  2. begin
  3. p := (2,3,5,7,11);
  4. // ... Do something with p.
  5. p := (13, 17, 19, 23, 29);
  6. // ... Do something else with p.
  7. end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 2395
  • Compiler Developer
Re: Arrays as parameters
« Reply #23 on: February 20, 2020, 09:51:52 am »
Ok thanks. So does that mean that this construct will not work to assign multiple values to normal static arrays? Because I'd love to be able to do that.

Say this code for example. Can this (or something similar) be done with a static array?

Code: Pascal  [Select][+][-]
  1. var  p[1..5] : array of Integer;
  2. begin
  3. p := [2,3,5,7,11]
  4. end;

This is currently not supported, though I already had the idea as well when implementing the dynamic array constructors. So maybe in the future...

 

TinyPortal © 2005-2018