Recent

Author Topic: Correct syntax for constructing open arrays of structured types  (Read 4281 times)

440bx

  • Hero Member
  • *****
  • Posts: 3946
Correct syntax for constructing open arrays of structured types
« on: September 15, 2018, 08:03:49 am »
Hello,

I would like to call a function that takes an open array of a structured type and construct the open array in the call to the function itself.  I have not been able to get the correct syntax.

Here is an example of what I'd like to do:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC                      }
  2.  
  3. program OpenArraysOfStructures;
  4.  
  5. type
  6.   TPoint = record
  7.     x        : integer;
  8.     y        : integer;
  9.   end;
  10.  
  11. var
  12.   t : TPoint = (x:1; y:1);
  13.  
  14.   t2 : array[0..1] of TPoint = ((x:1; y:1), (x:2; y:2));
  15.  
  16. function ProcessPoints(constref Points : array of TPoint) : boolean;
  17. begin
  18.   // process the points here
  19.  
  20.   result := true;
  21. end;
  22.  
  23. begin
  24.   ProcessPoints([t]);  // this works
  25.   ProcessPoints(t2);   // this works too
  26.  
  27.  
  28.   // this is what I really want to do but, the compiler doesn't like it.
  29.   // { call 3 }
  30.  
  31.   ProcessPoints([(x:1; y:1)]);  // tried a few different ways but none worked.
  32.  
  33. end.
  34.  

The question is: what's the correct syntax to build the array directly in the function call { call 3 } ?

Thank you.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

WooBean

  • Full Member
  • ***
  • Posts: 229
Re: Correct syntax for constructing open arrays of structured types
« Reply #1 on: September 15, 2018, 08:51:29 am »
Hi 440bx,
try this approache :

Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC                      }
  2.  
  3. program OpenArraysOfStructures;
  4.  
  5. type
  6.   TPoint = record
  7.     x        : integer;
  8.     y        : integer;
  9.   end;
  10.  
  11. function Point(x1,y1:Integer):TPoint;
  12. begin
  13.   result.x:=x1;
  14.   result.y:=y1;
  15. end;
  16.  
  17. var
  18.   t : TPoint = (x:1; y:1);
  19.  
  20.   t2 : array[0..1] of TPoint = ((x:1; y:1), (x:2; y:2));
  21.  
  22. function ProcessPoints(constref Points : array of TPoint) : boolean;
  23. begin
  24.   // process the points here
  25.  
  26.   result := true;
  27. end;
  28.  
  29. begin
  30.   ProcessPoints([t]);  // this works
  31.   ProcessPoints(t2);   // this works too
  32.  
  33.  
  34.   // this is what I really want to do but, the compiler doesn't like it.
  35.   // { call 3 }
  36.  
  37. //  ProcessPoints([(x:1; y:1)]);  // tried a few different ways but none worked.
  38.   ProcessPoints([Point(1,1)]);   // this works
  39. end.
  40.  
  41.  

Note that in a real world the Point function is already defined -

WooBean
Platforms: Win7/64, Linux Mint Ulyssa/64

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Correct syntax for constructing open arrays of structured types
« Reply #2 on: September 15, 2018, 10:42:14 am »
Hello WooBean.

Very nice workaround. 

If no one provides a way to build the structure directly in the function call, I will be using your solution.

Thanks.

PS: I know TPoint already exists but,  I just used it as an example everyone could related to, I'll be using a record which isn't predefined.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

andresayang

  • Full Member
  • ***
  • Posts: 108
Re: Correct syntax for constructing open arrays of structured types
« Reply #3 on: September 16, 2018, 09:17:20 pm »
Hello WooBean.

Very nice workaround. 

If no one provides a way to build the structure directly in the function call, I will be using your solution.

Thanks.

PS: I know TPoint already exists but,  I just used it as an example everyone could related to, I'll be using a record which isn't predefined.


It seems you have a trouble between passing parameters by values or by references.
I guess that when you use a dynamic array as function parameter, you must pass parameters by reference and not by values.

Cheers
Linux, Debian 12
Lazarus: always latest release

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Correct syntax for constructing open arrays of structured types
« Reply #4 on: September 16, 2018, 09:23:31 pm »
It seems you have a trouble between passing parameters by values or by references.
I guess that when you use a dynamic array as function parameter, you must pass parameters by reference and not by values.

Cheers
If you have an example, with or without constref, where the array is constructed directly in the function call, that would be great.  Make any corrections you need to make it work, as long as, the array is constructed in the call itself.

Thank you.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

andresayang

  • Full Member
  • ***
  • Posts: 108
Re: Correct syntax for constructing open arrays of structured types
« Reply #5 on: September 16, 2018, 09:54:17 pm »
Hi,

I guess

Code: Pascal  [Select][+][-]
  1. function ProcessPoints(Points : array [0..1] of TPoint) : boolean;

Will do what you like.
(Not tested)

About contref, from lazarus doc: The contref notes for version 2.6 suggest that this can be used for interfacing with external routines in other languages, where this type of parameter passing is required. Other uses of constref may hinder the compiler from optimizing code.
In other words: If you do not need to interface with "external lib or program" where this parameter is mandatory, do not use it.
« Last Edit: September 16, 2018, 10:04:14 pm by andresayang »
Linux, Debian 12
Lazarus: always latest release

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Correct syntax for constructing open arrays of structured types
« Reply #6 on: September 17, 2018, 12:31:27 am »
Hi,

I guess

Code: Pascal  [Select][+][-]
  1. function ProcessPoints(Points : array [0..1] of TPoint) : boolean;

Will do what you like.
(Not tested)
You should get in the habit of testing your suggestions.  It's not too late, try it.  Very nice that you can read the documentation, it will be very useful once you understand it.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Correct syntax for constructing open arrays of structured types
« Reply #7 on: September 17, 2018, 11:36:46 am »
Dunno, I think, it's supposed to be like this:
Code: Pascal  [Select][+][-]
  1. ProcessPoints([TPoint.Create(1, 1)]);
  2.  
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

andresayang

  • Full Member
  • ***
  • Posts: 108
Re: Correct syntax for constructing open arrays of structured types
« Reply #8 on: September 17, 2018, 02:21:35 pm »
Hi,

I guess

Code: Pascal  [Select][+][-]
  1. function ProcessPoints(Points : array [0..1] of TPoint) : boolean;

Will do what you like.
(Not tested)
You should get in the habit of testing your suggestions.  It's not too late, try it.  Very nice that you can read the documentation, it will be very useful once you understand it.

Well well. Ok, I do not test it because this was the way I was doing with Turbo-Pascal and before the ability to have dynamic arrays.
If it does not work, sorry ! I was only trying to help you. I know now, reading your answer, that I should not.

But your problem still my first answer, using the "magic word" constref or not, doesn't change nothing to your problem.
« Last Edit: September 17, 2018, 02:36:57 pm by andresayang »
Linux, Debian 12
Lazarus: always latest release

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Correct syntax for constructing open arrays of structured types
« Reply #9 on: September 17, 2018, 05:50:51 pm »
But your problem still my first answer, using the "magic word" constref or not, doesn't change nothing to your problem.
The use of constref has nothing to do with the question I asked.  In addition to that, the statement about constref from one of your previous posts:
About contref, from lazarus doc: The contref notes for version 2.6 suggest that this can be used for interfacing with external routines in other languages, where this type of parameter passing is required. Other uses of constref may hinder the compiler from optimizing code.

In other words: If you do not need to interface with "external lib or program" where this parameter is mandatory, do not use it.
is incorrect no matter where you got it from.  Write some sample programs using constref and look at the code that is generated with and without it.  That way, you'll know what it does.

Here is an excerpt from the Free Pascal Reference Guide (page 192)
Quote
Specifying a parameter as Constant is giving the compiler a hint that the contents of the parameter will not be changed by the called routine. This allows the compiler to perform optimizations which it could not do otherwise, and also to perform certain checks on the code inside the routine: namely, it can forbid assignments to the parameter. Furthermore a const parameter cannot be passed on to another function that requires a variable parameter: the compiler can check this as well. The main use for this is reducing the stack size, hence improving performance, and still retaining the semantics of passing by value...

Remark: Contrary to Delphi, no assumptions should be made about how const parameters are passed to the underlying routine. In particular, the assumption that parameters with large size are passed by reference is not correct. For this the constref parameter type should be used, which is available as of version 2.5.1 of the compiler.
An exception is the stdcall calling convention: for compatibility with COM standards, large const parameters are passed by reference.
IOW, it's darn close to the opposite of what you stated.  Not only you don't answer the question that has been asked but, in addition to that, you give incorrect information.  That is not the definition of being helpful.  If you want to be helpful, make sure what you suggest is correct and works by testing it before you post it.

BTW, what the Free Pascal Reference Guide states is correct, I tested it.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018