Recent

Author Topic: Initializing Record with Array Content  (Read 3245 times)

local-vision

  • Jr. Member
  • **
  • Posts: 77
Initializing Record with Array Content
« on: July 23, 2024, 08:05:04 pm »
So I am able to initialize the variable S1 record with the array AOS with a set of Strings.

But if want to do the same with an array that I declared before that, it fails.

Any ideas?

Code: Pascal  [Select][+][-]
  1. type Tar=record
  2.        AOS: array of string;
  3.      end;
  4.  
  5. const  List1: array[0..2] of String = ('First','Second','Third');
  6.        List2: array of String = ('First','Second','Third');
  7.  
  8.        S1:Tar=(AOS:('First','Second','Third')); // Works
  9.        S2:Tar=(AOS:List1); // Error: Syntax error, "(" expected but "identifier LIST1" found
  10.        S3:Tar=(AOS:List2); // Error: Syntax error, "(" expected but "identifier LIST2" found
  11.  

korba812

  • Sr. Member
  • ****
  • Posts: 442
Re: Initializing Record with Array Content
« Reply #1 on: July 23, 2024, 08:45:06 pm »
This limitation is probably due to the fact that typed constants are like variables.
https://www.freepascal.org/docs-html/ref/refse10.html
I don't know if or how to get around it. You probably have to live with it  ;).

local-vision

  • Jr. Member
  • **
  • Posts: 77
Re: Initializing Record with Array Content
« Reply #2 on: July 23, 2024, 09:19:11 pm »
What about this:

Code: Pascal  [Select][+][-]
  1. type Tar=record
  2.        AOS: array of string;
  3.      end;
  4.  
  5. const  List1: array[0..2] of String = ('First','Second','Third');
  6.        List2: array of String = ('First','Second','Third');
  7.  
  8.  var S1:Tar=(AOS:('First','Second','Third')); // Works
  9.        S2:Tar=(AOS:List1); // Error: Syntax error, "(" expected but "identifier LIST1" found
  10.        S3:Tar=(AOS:List2); // Error: Syntax error, "(" expected but "identifier LIST2" found
  11.  

I get the same situation

korba812

  • Sr. Member
  • ****
  • Posts: 442
Re: Initializing Record with Array Content
« Reply #3 on: July 23, 2024, 09:36:11 pm »
Analogous to what I wrote earlier: initialized variables are similar to typed constants, the difference is when initialization occurs.
https://www.freepascal.org/docs-html/ref/refse24.html#x55-750004.4
And you can't use variable to initialize a constant (edit: maybe with exceptions, like a pointer to a variable...).
« Last Edit: July 23, 2024, 09:39:52 pm by korba812 »

local-vision

  • Jr. Member
  • **
  • Posts: 77
Re: Initializing Record with Array Content
« Reply #4 on: July 23, 2024, 11:31:44 pm »
Appreciated the prompt reply and info. Reviewing.
 
And you can't use variable to initialize a constant (edit: maybe with exceptions, like a pointer to a variable...).

It seems that what I am attempting to achieve is not really possible.

TRon

  • Hero Member
  • *****
  • Posts: 3623
Re: Initializing Record with Array Content
« Reply #5 on: July 24, 2024, 12:12:57 am »
It seems that what I am attempting to achieve is not really possible.
That is correct.
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Initializing Record with Array Content
« Reply #6 on: July 24, 2024, 12:22:19 am »
I believe constants needs to be defined at one point, here you are asking the compiler to look back and something that most of us don't want to do  "Look back"  :o

However, you could use compiler intrinsic to choose from build to build as to what string goes in there.

Not sure what you are up to but, you define that at runtime because they are writable constants.
The only true wisdom is knowing you know nothing

cpicanco

  • Hero Member
  • *****
  • Posts: 655
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Initializing Record with Array Content
« Reply #7 on: July 24, 2024, 12:52:21 am »
I am used to solve this problem initializing variables with constant values using the initialization section. What do you thing about it? For example:

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. uses MyTypes;
  4.  
  5. const
  6.   LStrings: array [0..2] of string = ('First', 'Second', 'Third');
  7.  
  8. var
  9.   MyRecord : TMyRecord = (AOS: nil );
  10.   i : integer;
  11.  
  12. initialization
  13.   SetLength(MyRecord.AOS, Length(LStrings));
  14.   for i := Low(LStrings) to High(LStrings) do begin
  15.     MyRecord.AOS[i] := LStrings[i];
  16.   end;
« Last Edit: July 24, 2024, 10:23:33 am by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

Thaddy

  • Hero Member
  • *****
  • Posts: 16157
  • Censorship about opinions does not belong here.
Re: Initializing Record with Array Content
« Reply #8 on: July 24, 2024, 10:59:25 am »
 :) That is a good one.
Another one is in which mode?
I mean () notation vs [] notation?

Just a thought....

BTW: you can also initialize a record as a typed const, which is subsequently edittable in{$J+} state. Or using the already mentioned management operators.
« Last Edit: July 24, 2024, 11:02:13 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

cpicanco

  • Hero Member
  • *****
  • Posts: 655
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Initializing Record with Array Content
« Reply #9 on: July 24, 2024, 11:10:24 am »
Do you mean this?

Code: Pascal  [Select][+][-]
  1. unit constants;
  2.  
  3. interface
  4.  
  5. uses MyTypes;
  6.  
  7. const
  8.   MyRecord : TMyRecord = (AOS: nil );
  9.  
  10. implementation
  11.  
  12. const
  13.   LStrings: array [0..2] of string = ('First', 'Second', 'Third');
  14.   i : integer;
  15.  
  16. initialization
  17.   {$WRITEABLECONST ON}
  18.   SetLength(MyRecord.AOS, Length(LStrings));
  19.   for i := Low(LStrings) to High(LStrings) do begin
  20.     MyRecord.AOS[i] := LStrings[i];
  21.   end;
  22.   {$WRITEABLECONST OFF}
  23. end.
  24.  

Quote
Another one is in which mode?
I mean () notation vs [] notation?

I am not sure what do you mean.
« Last Edit: July 24, 2024, 11:12:06 am by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

Thaddy

  • Hero Member
  • *****
  • Posts: 16157
  • Censorship about opinions does not belong here.
Re: Initializing Record with Array Content
« Reply #10 on: July 24, 2024, 02:13:49 pm »
Array initialization between {$mode objfpc} and {$mode delphi} differs.
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1646
    • http://www.cdbc.dk
Re: Initializing Record with Array Content
« Reply #11 on: July 24, 2024, 02:18:19 pm »
Hi
@cpicanco: I think Thaddy means more like this:
Code: Pascal  [Select][+][-]
  1.   PLogDirsCreated = ^TLogDirsCreated;
  2.   TLogDirsCreated = record
  3.    const ldcDirs: TStringArray = ('common','models','presenters','views');
  4.    var
  5.     ldcAbbreviation,    // user's 3 letter abbreviation of the project-name
  6.     ldcLogText: string; // user targeted msg
  7.     ldcProjectname: string; // if empty, we'll extract the last directory from root, depending on below
  8.     ldcProjectnameAsRoot: boolean; // and vice versa -> extract projectname from root
  9.     ldcResult: boolean; // well DUH!
  10.     ldcRoot: string; // where do we start...
  11.     procedure AddDirectory(const aName: string);
  12.     procedure AssignDirs(aSrc: TStrings);
  13.     function CountDirs: integer;
  14.     procedure GetDirsAsStrings(aList: TStrings);
  15.     function IndexOfDir(const aName: string): integer;
  16.     procedure RemoveDirectory(anIdx: integer);
  17.   end;
works like a charm in {$J+} mode objfpc
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16157
  • Censorship about opinions does not belong here.
Re: Initializing Record with Array Content
« Reply #12 on: July 24, 2024, 02:23:34 pm »
Thx Benny,

I am too sad today.

One of, no, the greatest hero of mine sadly died today.
Piece of great importance to musical history.
Code: Pascal  [Select][+][-]
  1. begin
  2.   writeln('Hello, Beano');
  3. end.
Few of you will understand this code anyway.
« Last Edit: July 24, 2024, 03:07:13 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1646
    • http://www.cdbc.dk
Re: Initializing Record with Array Content
« Reply #13 on: July 24, 2024, 02:30:33 pm »
Hi Thaddy
I guess 'Sorry for your/our loss' is appropriate here.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16157
  • Censorship about opinions does not belong here.
Re: Initializing Record with Array Content
« Reply #14 on: July 24, 2024, 04:12:50 pm »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018