Recent

Author Topic: [Solved] What is wrong with my record declaration.  (Read 626 times)

BrunoK

  • Hero Member
  • *****
  • Posts: 570
  • Retired programmer
[Solved] What is wrong with my record declaration.
« on: September 07, 2024, 12:44:26 pm »
The following program compiles (kept a bit of non concerned code) :
Code: Pascal  [Select][+][-]
  1. program pgmCasedRecord;
  2.  
  3. {$mode Delphi}{$H+}
  4. {$Modeswitch advancedrecords}
  5.  
  6. type
  7.   PDWordSub = ^RDWordSub;
  8.  
  9.   { RDWordSub }
  10.  
  11.   RDWordSub = packed record
  12.   public
  13.   type
  14.     TSubsType =
  15.     (stSet0,     // Work on set pairs : Record belongs to set 0
  16.      stSet1);    //
  17.     TSubsTypes = set of TSubsType;
  18.   private
  19.     function InitBound(aDw: DWord; aRngHead: boolean): PDWordSub;
  20.   public
  21.     case boolean of
  22.       False:
  23.         (dw: DWord;        // DWord boundary
  24.          ss: shortint;     // set id, bitset [0..6] indicating which set holds the record
  25.                            // [0] is the lead set.
  26.          h: shortint);     // 0 -> start of subset , 1 (or >1)-> end of subset
  27.       True:
  28.         (plim: PDWordSub); // Used to store work in list pointers
  29.   end;
  30. const
  31.   cMaxWSubs = 1 shl 12; // 2^12 4 k items -> 2k subranges
  32.  
  33. type { Work array created 'on the fly' by routines needing range manipulation }
  34.   TArSubs = array[0..cMaxWSubs] of RDWordSub; // [cMaxWSubs]=Count(used).
  35.  
  36. { RDWordSub }
  37.  
  38. function RDWordSub.InitBound(aDw: DWord; aRngHead: boolean): PDWordSub;
  39. begin
  40.   Result := nil;
  41. end;
  42.  
  43. begin
  44.  
  45. end.
But this one with  the private (same if it had been public btw) declaration moved after the case boolean fails with messages
project1.lpr(27,3) Error: Identifier not found "private"
project1.lpr(28,5) Error: Illegal expression
project1.lpr(28,5) Error: Syntax error, ":" expected but "FUNCTION" found

Code: Pascal  [Select][+][-]
  1. program pgmCasedRecord;
  2.  
  3. {$mode Delphi}{$H+}
  4. {$Modeswitch advancedrecords}
  5.  
  6. type
  7.   PDWordSub = ^RDWordSub;
  8.  
  9.   { RDWordSub }
  10.  
  11.   RDWordSub = packed record
  12.   public
  13.   type
  14.     TSubsType =
  15.     (stSet0,     // Work on set pairs : Record belongs to set 0
  16.      stSet1);    //
  17.     TSubsTypes = set of TSubsType;
  18.   public
  19.     case boolean of
  20.       False:
  21.         (dw: DWord;        // DWord boundary
  22.          ss: shortint;     // set id, bitset [0..6] indicating which set holds the record
  23.                            // [0] is the lead set.
  24.          h: shortint);     // 0 -> start of subset , 1 (or >1)-> end of subset
  25.       True:
  26.         (plim: PDWordSub); // Used to store work in list pointers
  27.   private
  28.     function InitBound(aDw: DWord; aRngHead: boolean): PDWordSub;
  29.   end;
  30. const
  31.   cMaxWSubs = 1 shl 12; // 2^12 4 k items -> 2k subranges
  32.  
  33. type { Work array created 'on the fly' by routines needing range manipulation }
  34.   TArSubs = array[0..cMaxWSubs] of RDWordSub; // [cMaxWSubs]=Count(used).
  35.  
  36. { RDWordSub }
  37.  
  38. function RDWordSub.InitBound(aDw: DWord; aRngHead: boolean): PDWordSub;
  39. begin
  40.   Result := nil;
  41. end;
  42.  
  43. begin
  44.  
  45. end.

What is wrong with the second declaration of case of boolean ?
« Last Edit: September 07, 2024, 01:38:49 pm by BrunoK »

bytebites

  • Hero Member
  • *****
  • Posts: 672
Re: What is wrong with my record declaration.
« Reply #1 on: September 07, 2024, 12:56:50 pm »
Variant part must last in record declaration?

BrunoK

  • Hero Member
  • *****
  • Posts: 570
  • Retired programmer
Re: What is wrong with my record declaration.
« Reply #2 on: September 07, 2024, 01:38:28 pm »
Variant part must last in record declaration?
Thank you, that's it. The documentation  https://www.freepascal.org/docs-html/ref/refsu15.html states :
Quote
The variant part must be last in the record.
I just, wrongly, expected that advanced methods would not be considered as part of the record data.

jamie

  • Hero Member
  • *****
  • Posts: 6550
Re: [Solved] What is wrong with my record declaration.
« Reply #3 on: September 07, 2024, 04:13:35 pm »
If you want a Variant record in the middle of your record with more data following, you need to wrap it with a record within.

of Course, this adds a name space in your record since you can't have anonymous record in there for nameless unions.

But I am sure you already know this.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018