[fpc-pascal] Union field after property violates "field before property" rule + property of union field not possible.

Skybuck Flying skybuck2000 at hotmail.com
Sun Aug 14 11:49:01 CEST 2022


Delphi and presumably Free Pascal has some language inconsistency and problem/short coming concerning "Union Field" and/or "Union Property":

The (previous) presented solution (included at end of post) is inconsistent in two ways:

1. Property of union not possible.

2. Order of field and property violations "field before property" rule.

(Both problems demonstrated in TDataExample3)

{

version 0.03 created on 14 august 2022 by Skybuck Flying:

Today I remember the rule which this union field is conflicting with.

The rule: "field before property"

Going to demonstrate it below

There are apperently two problems with this solution:

1. Property of union not possible ?!?

2. Inconsistent language design in respect to standard field + property relation
(order of appearance/declaration)

// ERROR "field definition not allowed after methods or properties" problem 2.

}


type
      TDataExample3 = record

//          mStandardField : integer;  // OK

            // normal/standard property
            property StandardField : integer read mStandardField write mStandardField;

            mStandardField : integer;  // ERROR "field definition not allowed after methods or properties" problem 2.


            // union property
//          property UnionField : int64 read mData write mData;  // not possible, problem 1

            // union fields
            case integer of   // INCONSISTENT problem 2
                  0 : ( mData : int64 );
                  1 : ( mByte : packed array[0..7] of byte );

//          property UnionField : int64 read mData write mData;  // not possible, problem 1

      end;


The original problem was:

(*
      // ORIGINAL PROBLEM:
      TDataExample0 = record
      private
            mField : integer;
      public
            // UNION example
            case integer of
                  0 : ( mData : int64 );
                  1 : ( mByte : packed array[0..7] of byte );

            property Field read mField write mField; // DOES NOT COMPILE.
      end;
*)

      // UNION DECLARATION example that does work without properties, just to check"

      TDataExample1 = record
      private
            mField : integer;

      public
            // UNION example
            case integer of
                  0 : ( mData : int64 );
                  1 : ( mByte : packed array[0..7] of byte );

            // DOES COMPILE
      end;

Presented solution for union field in record with properties was:

      TDataExample2 = record
      private
            mField : integer;

      public
            // SOLUTION:
            property Field : integer read mField write mField;

            // PUT CASE STATEMENT LAST
            case integer of
                  0 : ( mData : int64 );
                  1 : ( mByte : packed array[0..7] of byte );

      end;

Bye,
  Skybuck.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20220814/9c66c735/attachment.htm>


More information about the fpc-pascal mailing list