Recent

Author Topic: Record helper allowed only before variant record?  (Read 421 times)

avra

  • Hero Member
  • *****
  • Posts: 2152
    • Additional info
Record helper allowed only before variant record?
« on: April 06, 2021, 09:30:12 am »
I know that variant part must be the at the end of the record, but I did not expect that in such case record helper can not be after variant part. Is there a way to put record helper after variant part (without putting the whole case block into another sub-record)?

Code: Pascal  [Select][+][-]
  1.   TMyRec = record
  2.     case byte of
  3.       1: ( a: byte; b: byte );
  4.       2: ( c: word );
  5.     procedure SetName(const AName: string); // not allowed "Error: Illegal expression"
  6.   end;
  7.  

Code: Pascal  [Select][+][-]
  1.   TMyRec = record
  2.     procedure SetName(const AName: string); // allowed
  3.     case byte of
  4.       1: ( a: byte; b: byte );
  5.       2: ( c: word );
  6.   end;
  7.  
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

WooBean

  • Full Member
  • ***
  • Posts: 112
Re: Record helper allowed only before variant record?
« Reply #1 on: April 06, 2021, 10:34:56 am »
Hi,

FPC documetation about advanced records (https://www.freepascal.org/docs-html/ref/refse60.html#x119-1410009.1) says:

Remark: In the case of a variant record, the Case keyword implicitly starts a var section, meaning class variables or methods are not allowed in the variant part of the record.

It is not the best part of FPC documentation IMHO but must be enough.
A working approach for advanced records is:  the last must be a variant part (if not omitted).

BTW - record helpers have nothing to do with the question.
« Last Edit: April 06, 2021, 10:52:25 am by WooBean »
Win7/64, Lazarus 2.0.8 win64-win64, FPC 3.0.4

PascalDragon

  • Hero Member
  • *****
  • Posts: 2959
  • Compiler Developer
Re: Record helper allowed only before variant record?
« Reply #2 on: April 06, 2021, 01:15:22 pm »
I know that variant part must be the at the end of the record, but I did not expect that in such case record helper can not be after variant part. Is there a way to put record helper after variant part (without putting the whole case block into another sub-record)?

First of, that is not a record helper, but a method. A record helper is something completely different.

Second, the variant part must be last.

You could however use a record helper to avoid this problem. ;)

Code: Pascal  [Select][+][-]
  1. type
  2.   TMyRec = record
  3.     case Byte of
  4.       1: (a: Byte; b: Byte);
  5.       2: (c: Word);
  6.   end;
  7.  
  8.   TMyRecHelper = record helper for TMyRec
  9.   public
  10.     procedure SetName(const aName: String);
  11.   end;

jamie

  • Hero Member
  • *****
  • Posts: 4440
Re: Record helper allowed only before variant record?
« Reply #3 on: April 06, 2021, 10:58:20 pm »
yes, real unions are needed … including those that are anonymous.

You can wrap the case with a Record then you can add items afterwards however, you now need a label for the record because you are not allowed to have nameless records wrapped around a variant like that..

Anonymous records are needed to fix the lack of having real unions.


 
The only true wisdom is knowing you know nothing

avra

  • Hero Member
  • *****
  • Posts: 2152
    • Additional info
Re: Record helper allowed only before variant record?
« Reply #4 on: April 07, 2021, 01:46:49 am »
BTW - record helpers have nothing to do with the question.
You're completely right. %) :-\ %) I tried so many things in order to find a work around that terms mixed up in my head when writing about it. Anyway, I didn't expect that limitation after variant part applies to extended record properties and methods, too.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jamie

  • Hero Member
  • *****
  • Posts: 4440
Re: Record helper allowed only before variant record?
« Reply #5 on: April 07, 2021, 02:44:02 am »
its lack of insight.. that is what the problem is..

They claim they must not exceed Delphi abilities yet there are things in FPC that Delphi can not do.

but when it comes to expanding the language to make it more serious then the old excuse of "If Delphi does not do it we are not doing it "

 I think the real issue is they are afraid to blow the cobwebs off the old code base and dig into it to build it up..

 A real union is needed for pascal. I would bet if code was produced using fpc with these new features that Delphi would follow suit..

 as it is now, Delphi does have things that FPC does not and I don't see them working on putting it in..

 but what I do see is bugs in the newer compilers that were not there before..

 so you attempt to put in new features and create bugs with the existing features while you are at it.

 I guess it goes hand to hand these days
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018