How do I define implicit conversion operators for mutually dependent records?

前端 未结 2 1711
小鲜肉
小鲜肉 2020-12-17 16:19

I am using the operator overloading for records in Delphi 2006. (Please don\'t answer this question by telling me not to.)

I have two record types with the impli

相关标签:
2条回答
  • 2020-12-17 16:41

    You can't have forward declarations for record types. Define both Implicit operators in the second type:

    type
      TMyRec1 = record
        Field1 : Integer;
      end;
    
      TMyRec2 = record
        Field2: Integer;
        class operator Implicit(a: TMyRec2): TMyRec1;
        class operator Implicit(a: TMyRec1): TMyRec2;
      end;
    

    Quoting from the help:

    Implicit conversions should be provided only where absolutely necessary, and reflexivity should be avoided. It is best to let type B implicitly convert itself to type A, and let type A have no knowledge of type B (or vice versa).

    0 讨论(0)
  • 2020-12-17 16:58

    You might be able to do this with record helpers.

    Below is what I recently did to work around the impossibility of having a forward record record declaration.

    It uses the record helper construct, which - like implicit type casts - have drawbacks too.
    The most important one being that only the nearest record helper for a particular record type will apply.

    type
      TIpv4Address = record
      strict private
        FAddress: TIpv4Quad;
        FMask: TIpv4Quad;
      private
        class function CreateInternal(const IP_ADDR_STRING: _IP_ADDR_STRING): TIpv4Address; static;
      public
        class function Create(const IP_ADDR_STRING: _IP_ADDR_STRING): TIpv4Address; static;
        class function Count(const IP_ADDR_STRING: _IP_ADDR_STRING): Integer; static;
        property Address: TIpv4Quad read FAddress;
        property Mask: TIpv4Quad read FMask;
      end;
    
      TIpv4AddressList = array of TIpv4Address;
    
      TIpv4AddressHelper = record helper for TIpv4Address
        class function CreateList(const IP_ADDR_STRING: _IP_ADDR_STRING): TIpv4AddressList; static;
      end;
    

    You use it like this:

    function TAdapterInfo.GetIpAddressList: TIpv4AddressList;
    begin
      Result := TIpv4Address.CreateList(AdapterInfos.IP_ADAPTER_INFO[Index].IpAddressList);
    end;
    

    --jeroen

    0 讨论(0)
提交回复
热议问题