Recent

Author Topic: Initialize list in function parameter  (Read 1509 times)

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: Initialize list in function parameter
« Reply #15 on: November 09, 2025, 03:40:16 pm »
Bear in mind, you are now using variant records which last time I checked are now 24 bytes each, they used to be like 16 at one time at one time but who is counting.

 so, if you are planning on a large collection of these items, beware.

Jamie
The only true wisdom is knowing you know nothing

PeterHu

  • Jr. Member
  • **
  • Posts: 60
Re: Initialize list in function parameter
« Reply #16 on: November 10, 2025, 10:33:27 am »
Thank you all again.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6235
  • Compiler Developer
Re: Initialize list in function parameter
« Reply #17 on: November 10, 2025, 09:34:38 pm »
Thanks.This version is easier to understand to me.I see it as if data( const array of const) is make up of 'Variant' type.

array of const is different from Variant. While both rely on the concept of variant records, array of const is based on TVarRec and does not provide operators. It's only really intended for parameter passing, while Variant has a much wider scope due to functionality like late binding and provided operator overloads.

PeterHu

  • Jr. Member
  • **
  • Posts: 60
Re: Initialize list in function parameter
« Reply #18 on: November 11, 2025, 01:35:54 am »
Thanks.This version is easier to understand to me.I see it as if data( const array of const) is make up of 'Variant' type.

array of const is different from Variant. While both rely on the concept of variant records, array of const is based on TVarRec and does not provide operators. It's only really intended for parameter passing, while Variant has a much wider scope due to functionality like late binding and provided operator overloads.

Got it,thank you.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 305
  • I use FPC [main] 💪🐯💪
Re: Initialize list in function parameter
« Reply #19 on: November 11, 2025, 05:10:25 am »
Yes, code using Variants looks even neater

Code: Pascal  [Select][+][-]
  1. program app;
  2. {$mode objfpc}
  3. //{$codepage utf8}
  4. {$modeswitch advancedrecords}
  5. {$h+}
  6.  
  7. uses gvector;
  8.  
  9. type
  10.   TFruit = record
  11.     name: String;
  12.     color: Integer;
  13.     class operator :=(const data: array of Variant): TFruit;
  14.   end;
  15.  
  16. class operator TFruit.:=(const data: array of Variant): TFruit;
  17. begin
  18.   Result.name:=data[0];
  19.   Result.color:=data[1];
  20. end;
  21.  
  22. procedure print(s: String);
  23. begin
  24.   WriteLn(s);
  25. end;
  26.  
  27. var
  28.   V: Variant; // It is necessary due to a bug https://gitlab.com/freepascal.org/fpc/source/-/issues/41492
  29.  
  30. begin
  31.   with (specialize TVector<TFruit>.Create) do
  32.   begin
  33.     pushback(['apple', 1]);
  34.     WriteLn(items[0].name);
  35.     Free;
  36.   end;
  37.  
  38.   print('apple');
  39.   ReadLn;
  40. end.
  41.  
I may seem rude - please don't take it personally

PeterHu

  • Jr. Member
  • **
  • Posts: 60
Re: Initialize list in function parameter
« Reply #20 on: November 11, 2025, 06:44:38 am »
Yes, code using Variants looks even neater

Code: Pascal  [Select][+][-]
  1. program app;
  2. {$mode objfpc}
  3. //{$codepage utf8}
  4. {$modeswitch advancedrecords}
  5. {$h+}
  6.  
  7. uses gvector;
  8.  
  9. type
  10.   TFruit = record
  11.     name: String;
  12.     color: Integer;
  13.     class operator :=(const data: array of Variant): TFruit;
  14.   end;
  15.  
  16. class operator TFruit.:=(const data: array of Variant): TFruit;
  17. begin
  18.   Result.name:=data[0];
  19.   Result.color:=data[1];
  20. end;
  21.  
  22. procedure print(s: String);
  23. begin
  24.   WriteLn(s);
  25. end;
  26.  
  27. var
  28.   V: Variant; // It is necessary due to a bug https://gitlab.com/freepascal.org/fpc/source/-/issues/41492
  29.  
  30. begin
  31.   with (specialize TVector<TFruit>.Create) do
  32.   begin
  33.     pushback(['apple', 1]);
  34.     WriteLn(items[0].name);
  35.     Free;
  36.   end;
  37.  
  38.   print('apple');
  39.   ReadLn;
  40. end.
  41.  

This is good!  {KISS}

 

TinyPortal © 2005-2018