Search code examples
delphidelphi-xe2records

Complex records and private default property


I have a following record:

type
  TMongoIdArray = array[0..11] of Byte;

  TMongoId = record
  strict private
    FMongoIdArray: TMongoIdArray;

    function GetMongoIdByte(Index: Integer): Byte;
    procedure SetMongoIdByte(Index: Integer; const Value: Byte);
    function GetMemory: pointer;

    property MongoIdArray[Index: Integer]: Byte read GetMongoIdByte
        write SetMongoIdByte; default;
  public
    class operator Implicit(const AMongoId: TMongoIdArray): TMongoId;
    class operator Implicit(const APointer: pointer): TMongoId;
    class operator Implicit(const AVariant: Variant): TMongoId;
    class operator Implicit(const AString: String): TMongoId;
    class operator Equal(const AMongoId1, AMongoId2: TMongoId): Boolean;
    class operator NotEqual(const AMongoId1, AMongoId2: TMongoId): Boolean;

    function ToDateTime: TDateTime;
    function ToVariant: Variant;
    function ToString: String;
    function IsEmpty: Boolean;
    procedure Clear;

    property Memory: pointer read GetMemory;
  end;

I don't want user to see MongoIdArray property when trying to access record methods/properties outside of this unit, and solved this by putting default property in strict private section. Is this common (and recommended) practice?

Update from comments

I'm asking if its common practice to put default property under private/strict private to hide it from being accessed anywhere in the code (not only local unit).


Solution

  • I'm asking if its common practice to put default property under private/strict private to hide it from being accessed anywhere in the code (not only local unit).

    The visibility specifiers (private and strict private are visibility specifiers) are the only way to control which symbols can be seen by the rest of your program. So, it's not so much whether or not this is common practice, rather there is simply no other way to do this.