Recent

Author Topic: Dynamic arrays and literal record syntax  (Read 1289 times)

jouborg

  • New Member
  • *
  • Posts: 11
Dynamic arrays and literal record syntax
« on: December 31, 2020, 06:29:33 pm »
Dynamic array constants and variable initialization allow for succinct constant record array declaration (example in wiki). For example, I can do:

Code: Pascal  [Select][+][-]
  1. procedure Test1;
  2. type
  3.   TPerson = record
  4.     Name: String;
  5.     City: String;
  6.   end;
  7.  
  8.   TPeople = array of TPerson;
  9.  
  10. const
  11.   people: TPeople = (
  12.     (Name: 'Adam'; City: 'Los Angeles'),
  13.     (Name: 'Eve'; City: 'Berlin')
  14.   );
  15.  
  16. begin
  17.   { some code }
  18. end;
  19.  

However, it doesn't appear that you can initialize a variable that's an array of records with literal syntax. I presume that's because the "literal record syntax" and type inference is limited to the const section:

Code: Pascal  [Select][+][-]
  1. procedure Test3;
  2. type
  3.   TPerson = record
  4.     Name: String;
  5.     City: String;
  6.   end;
  7.  
  8.   TPeople = array of TPerson;
  9.  
  10. var
  11.   people: TPeople;
  12.  
  13. begin
  14.   { the code below fails, because there's no record literal syntax and type inference }
  15.   people := [
  16.     (Name: 'Adam'; City: 'Los Angeles'),
  17.     (Name: 'Eve'; City: 'Berlin')
  18.   ];
  19. end;      
  20.  

Is my understanding correct and, if so, has support for this been considered?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Dynamic arrays and literal record syntax
« Reply #1 on: December 31, 2020, 09:21:45 pm »
Recent FPCs accept this:
Code: Pascal  [Select][+][-]
  1. procedure Test3;
  2. type
  3.   TPerson = record
  4.     Name: String;
  5.     City: String;
  6.   end;
  7.  
  8.   TPeople = array of TPerson;
  9.  
  10. var
  11.   people: TPeople = ((Name: 'Adam'; City: 'Los Angeles'),
  12.                      (Name: 'Eve'; City: 'Berlin'));
  13. begin
  14.   // code to use people ...
  15. end;

jouborg

  • New Member
  • *
  • Posts: 11
Re: Dynamic arrays and literal record syntax
« Reply #2 on: January 01, 2021, 11:37:27 am »
Quote
Recent FPCs accept this

Only works at variable declaration time, right?

Doesn't seem to be valid in a function/procedure body (or the main program body), which is where it would be extra helpful, in my view.

So I cannot do:

Code: Pascal  [Select][+][-]
  1. program recordliterals;
  2.  
  3. type
  4.    TPerson = record
  5.       name: string;
  6.       city: string;
  7.    end;
  8.  
  9.    TPeople = array of TPerson;
  10.  
  11. var people: TPeople;
  12.  
  13. begin
  14.    people = ((Name: 'Adam'; City: 'Los Angeles'),
  15.              (Name: 'Eve'; City: 'Berlin'));
  16. end.
  17.  








PascalDragon

  • Hero Member
  • *****
  • Posts: 5481
  • Compiler Developer
Re: Dynamic arrays and literal record syntax
« Reply #3 on: January 01, 2021, 11:41:53 am »
Is my understanding correct and, if so, has support for this been considered?

Your understanding is correct. Support for such a syntax would not be possible, because the compiler would not know the type on the right side (it would need to work in more cases than just “simple” assignments). If you want something like that, write yourself a constructor for your record.

 

TinyPortal © 2005-2018