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

Skybuck Flying skybuck2000 at hotmail.com
Mon Aug 1 18:09:43 CEST 2022


program TestProgram3;

{

Test program to illustrate the 'union' followed by property problem in Delphi
language and Free Pascal Language

version 0.01 created on 1 august 2022 by Skybuck Flying

There is a problem with "unions" in Delphi language:

When a property follows a "union" declaration it does not compile !

See
TDataExample1 which does compile (no property)
vs
TDataExample2 which does NOT compile (with property)

}

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  Classes, SysUtils, CustApp
  { you can add units after this };

type

  { TMyApplication }

  TMyApplication = class(TCustomApplication)
  protected
    procedure DoRun; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure WriteHelp; virtual;
  end;

{ TMyApplication }

type
      TDataExample1 = record
            mField : integer;

            // 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;

procedure TMyApplication.DoRun;
var
  ErrorMsg: String;
begin
  // quick check parameters
  ErrorMsg:=CheckOptions('h', 'help');
  if ErrorMsg<>'' then begin
    ShowException(Exception.Create(ErrorMsg));
    Terminate;
    Exit;
  end;

  // parse parameters
  if HasOption('h', 'help') then begin
    WriteHelp;
    Terminate;
    Exit;
  end;

  { add your program here }

  // stop program loop
  Terminate;
end;

constructor TMyApplication.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  StopOnException:=True;
end;

destructor TMyApplication.Destroy;
begin
  inherited Destroy;
end;

procedure TMyApplication.WriteHelp;
begin
  { add your help code here }
  writeln('Usage: ', ExeName, ' -h');
end;

var
  Application: TMyApplication;
begin
  Application:=TMyApplication.Create(nil);
  Application.Title:='My Application';
  Application.Run;
  Application.Free;
end.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220801/426d3954/attachment.htm>


More information about the fpc-pascal mailing list