E2010 Incompatible types: T and string

Giganews Newsgroups
Subject:E2010 Incompatible types: T and string
Posted by: Ross Glenn
Date:Sun, 29 Aug 2010

Hi All

I am getting this error when trying to implement a generic class - *E2010 Incompatible types: T and string*

Version: Delphi 2010
Updates: - Delphi and C++ Builder 2010 update 5 (Database Pack)
              - RAD Studio 2010 Help Update 3
              - RAD Studio 2010 Update 1

Synopsis: Depending on the type(T) i am calling a specific method from the base class to load a specific data type. So, if the type of (T) is integer it will call a function to return an integer value. As you will see from the method (LoadAttribute) below. It accepts the primitive types of integer and float but will not compile for string. If I remove the record constraint then it throws an error for all primitive types? Your help is greatly appreciated.

If this is the incorrect way of doing this sort of thing, which would be the correct way to do this? Also, is Generics the best solution for this, it would seem so, else I would need a class or method for each data type?

I have pasted the code below. Any help would be greatly appreciated.

uses
  Generics.Collections, SysUtils, unFWDomainBase, unCoreXML, XMLIntf, unFWDomainConstants,
  TypInfo, Generics.Defaults;

type
  TDomainAttributeGeneric<T: record> = class(TBaseReplicatableDomainObject)
  private
    FAttributeCD    : integer;
    FAttributeValue : T;

    function GetAttributeValue: T;

    procedure SetAttributeValue(const AValue: T);
    function GetAttributeCD: integer;
    procedure SetAttributeCD(const AValue: integer);
  public
    function Replicate(AParent: TBaseDiscoveryLifeDomain): TBaseDiscoveryLifeDomain; override;

    procedure CopyFrom(AObject: TObject); override;
    procedure LoadAttribute(AXML: TCoreXML; AXmlNode: IXmlNode);
    procedure SaveAttribute(AXML: TCoreXML; AXmlNode: IXmlNode);

    property AttributeCD    : integer read GetAttributeCD    write SetAttributeCD;
    property AttributeValue : T      read GetAttributeValue  write SetAttributeValue;
  end;

implementation

{ TDomainAttributeGeneric<T> }

procedure TDomainAttributeGeneric<T>.LoadAttribute(AXML: TCoreXML; AXmlNode: IXmlNode);
var
  vKind: TTypeKind;
begin
  Clear;
  LoadBaseXML(AXML, AXmlNode);
  FAttributeCD    := AXML.GetTextNodeValueAsInt64(AXmlNode, CXMLNodeAttributeCD);
  vKind          := TTypeInfo(TypeInfo(T)^).Kind;
  if (vKind = tkInteger) then
  begin
    FAttributeValue := AXML.GetAttributeNodeValueAsInteger(AXmlNode, CXMLNodeAttributeVal1, CNullIntValue);
  end
  else if (VKind = tkFloat) then
  begin
    FAttributeValue := AXML.GetTextNodeValueAsFloat(AXmlNode, CXMLNodeAttributeVal1, CNullFloatValue);
  end
  else if (vKind = tkUString) then
  begin
    *// ***************************************
    *// code breaks here*
    *// ***************************************
    *FAttributeValue := AXML.GetTextNodeValueDef(AXmlNode, CXMLNodeAttributeVal1, CNullStrValue); // function returns string*
  end;
end;

Replies