[fpc-pascal] Union followed by Property does not compile !

Michael Van Canneyt michael at freepascal.org
Tue Aug 2 11:12:22 CEST 2022



On Mon, 1 Aug 2022, Skybuck Flying via fpc-pascal wrote:

> }
>
> {$mode objfpc}{$H+}

You need additionally a modeswitch for advanced records, or properties in
records will not work:

{$modeswitch advancedrecords}

>             // UNION example
>             case integer of
>                   0 : ( mData : int64 );
>                   1 : ( mByte : packed array[0..7] of byte );
>
>             // DOES COMPILE
>       end;
>
>       TDataExample2 = record
>            mField : integer;
>
>
>             // UNION example
>             case integer of
>                   0 : ( mData : int64 );
>                   1 : ( mByte : packed array[0..7] of byte );
>
>             // !!! DOES NOT COMPILE, PROBLEM !!!
>             property Field : integer read mField write mField;
>       end;

The variant part of a record must always come last. 
The property should be moved  to before the 'case' :

TDataExample2 = record
   mField : integer;
   property Field : integer read mField write mField;
   case integer of
   0 : ( mData : int64 );
   1 : ( mByte : packed array[0..7] of byte );
end;


This compiles when I tried.

Michael.


More information about the fpc-pascal mailing list