type
TObjA = class
a: string;
end;
type
worker<T> = interface
function step1(v: integer): T;
function step2(s: string): T;
end;
type
ImplA<T> = class(TInterfacedObject, worker<T>)
function step1(v: integer): T;
function step2(s: string): T; virtual; abstract;
end;
type
ImplB = class(ImplA<TObjA>)
function step2(s: string): TObjA;
end;
implementation
function ImplA<T>.step1(v: integer): T;
begin
result := step2(IntToStr(v));
end;
function ImplB.step2(s: string): TObjA;
var
r: TObjA;
begin
r := TObjA.Create;
r.a := 'step2 ' + s;
result := r;
end;
I am trying to build a functionality according to this structure. I know it works in java, but currently I am working in delphi 2010.
I get an abstract error when calling ImplB.step1(1)
How do I fix this?
You get the error as you do not declare function step2(s: string): TObjA; as an override.
So in
function ImplA<T>.step1(v: integer): T;
begin
result := step2(IntToStr(v));
end;
it is calling step2 from ImplA not ImplB as you are expecting it to
Your also changing the return type from a generic object to TObjA, the compiler may not like that, but I don't have a copy of Delphi that supports generics to hand to test.
Related
I have a Generic class which uses an Enum Generic Type. My problem how do I use GetEnumName on an instance of that type?
I've created a small demo class to illustrate the problem:
type
TEnumSettings<TKey: record > = class
private
Key: TKey;
public
constructor Create(aKey: TKey);
function ToString: string; override;
end;
uses
TypInfo;
{ TEnumSettings<TKey> }
constructor TEnumSettings<TKey>.Create(aKey: TKey);
begin
if PTypeInfo(System.TypeInfo(TKey)).Kind <> tkEnumeration then
Exception.Create(string(PTypeInfo(System.TypeInfo(TKey)).Name) + ' is not an Enumeration');
Key := aKey;
end;
function TEnumSettings<TKey>.ToString: string;
begin
Result := GetEnumName(System.TypeInfo(TKey), Integer(Key)) <== HERE I get a compile error: Invalid type cast
end;
I'm using Delphi XE. So can this be done? And if so how?
Personally, I would do this with a call to Move. I have the following type:
type
TEnumeration<T: record> = class
strict private
class function TypeInfo: PTypeInfo; inline; static;
class function TypeData: PTypeData; inline; static;
public
class function IsEnumeration: Boolean; static;
class function ToOrdinal(Enum: T): Integer; inline; static;
class function FromOrdinal(Value: Integer): T; inline; static;
class function MinValue: Integer; inline; static;
class function MaxValue: Integer; inline; static;
class function InRange(Value: Integer): Boolean; inline; static;
class function EnsureRange(Value: Integer): Integer; inline; static;
end;
{ TEnumeration<T> }
class function TEnumeration<T>.TypeInfo: PTypeInfo;
begin
Result := System.TypeInfo(T);
end;
class function TEnumeration<T>.TypeData: PTypeData;
begin
Result := TypInfo.GetTypeData(TypeInfo);
end;
class function TEnumeration<T>.IsEnumeration: Boolean;
begin
Result := TypeInfo.Kind=tkEnumeration;
end;
class function TEnumeration<T>.ToOrdinal(Enum: T): Integer;
begin
Assert(IsEnumeration);
Assert(SizeOf(Enum)<=SizeOf(Result));
Result := 0; // needed when SizeOf(Enum) < SizeOf(Result)
Move(Enum, Result, SizeOf(Enum));
Assert(InRange(Result));
end;
class function TEnumeration<T>.FromOrdinal(Value: Integer): T;
begin
Assert(IsEnumeration);
Assert(InRange(Value));
Assert(SizeOf(Result)<=SizeOf(Value));
Move(Value, Result, SizeOf(Result));
end;
class function TEnumeration<T>.MinValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MinValue;
end;
class function TEnumeration<T>.MaxValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MaxValue;
end;
class function TEnumeration<T>.InRange(Value: Integer): Boolean;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := Math.InRange(Value, ptd.MinValue, ptd.MaxValue);
end;
class function TEnumeration<T>.EnsureRange(Value: Integer): Integer;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := Math.EnsureRange(Value, ptd.MinValue, ptd.MaxValue);
end;
The ToOrdinal method does what you need, and I'm sure you'll be able to adapt it to your class.
If you don't like using Move in this way, then you can use TValue.
TValue.From<TKey>(Key).AsOrdinal
And #TLama points out that you can avoid calling GetEnumName at all by using
TValue.From<TKey>(Key).ToString
On the face of it, using TValue seems to be more in keeping with the ethos of generics and RTTI. A call to Move relies on the specific implementation details of enumerated types. However, it's quite interesting to step through the debugger and observe quite how much code is involved in executing TValue.From<TKey>(Key).AsOrdinal. That alone is enough to make me hesitate to recommend using TValue.
Yet another way to achieve this is to use TRttiEnumerationType:
TRttiEnumerationType.GetName<TKey>(Key)
The implementation of this is much more efficient than using TValue.ToString, being little more than a call to GetEnumName.
This is an updated version af my class, with the change suggested. Thanks to David and TLama
uses
TypInfo, Rtti;
type
TEnumSettings<TKey: record> = class
private
Key: TKey;
public
constructor Create(aKey: TKey);
function ToString: string; override;
end;
{ TEnumSettings<TKey> }
constructor TEnumSettings<TKey>.Create(aKey: TKey);
begin
if PTypeInfo(System.TypeInfo(TKey)).Kind <> tkEnumeration then
raise Exception.Create(string(PTypeInfo(System.TypeInfo(TKey)).Name) + ' is not an Enumeration');
Key := aKey;
end;
function TEnumSettings<TKey>.ToString: string;
begin
Result := TValue.From<TKey>(Key).ToString;
end;
And a little test example :
Copy the code into OnCreate of a From:
procedure TForm1.FormCreate(Sender: TObject);
begin
with TEnumSettings<boolean> .Create(True) do
try
Caption := ToString;
finally
Free;
end;
end;
I get an internal compile error with Delphi XE3 Update 2 when I execute the following code:
unit Unit1;
interface
type
IHasValueR<T> = interface
function GetValue: T;
end;
IHasValueRw<T> = interface(IHasValueR<T>)
procedure SetValue(NewValue: T);
end;
TDummy = class(TInterfacedObject)
end;
TRefObj = class(TInterfacedObject, IHasValueR<Boolean>, IHasValueRw<Boolean>)
strict private
Value: Boolean;
public
constructor Create(Init_: Boolean);
function GetValue: Boolean;
procedure SetValue(NewValue: Boolean);
end;
TValueProviderFct<T, V> = reference to function(Input: T): V;
TBar<T; V: IHasValueRw<Boolean>> = class
strict private
FValueProviderFct: TValueProviderFct<T, V>;
public
constructor Create(ValueProviderFct_: TValueProviderFct<T, V>);
function GetValue(Input: T): Boolean;
end;
procedure TestIt();
implementation
procedure TestIt();
var
Foo: TRefObj;
Bar: TBar<TRefObj, IHasValueRw<Boolean>>;
begin
Foo := TRefObj.Create(true);
Bar := TBar<TRefObj, IHasValueRw<Boolean>>.Create(
function (Input: TRefObj): IHasValueRw<Boolean>
begin
Result := Input;
end
);
Bar.GetValue(Foo);
end;
{ TSetupDefinitionItemBoolean }
constructor TRefObj.Create(Init_: Boolean);
begin
Value := Init_;
end;
function TRefObj.GetValue: Boolean;
begin
Result := Value;
end;
procedure TRefObj.SetValue(NewValue: Boolean);
begin
Value := NewValue;
end;
{ TBar<T, V> }
constructor TBar<T, V>.Create(ValueProviderFct_: TValueProviderFct<T, V>);
begin
FValueProviderFct := ValueProviderFct_;
end;
function TBar<T, V>.GetValue;
begin
Result := FValueProviderFct(Input).GetValue;
end;
end.
error-message:
[dcc32 Fatal Error] Unit1.pas(83): F2084 Internal Error: C13823
The solution is simply to add GUIDs to the interfaces.
Can anyone verify this?
Is it maybe already fixed in a newer Delphi version?
Where can we file a bugreport?
Can anyone verify this? Is it maybe already fixed in a newer Delphi version?
For the record:
This is fixed in Delphi XE4.
It also compiles just fine in XE2, so it seems to be specific to your version.
Where can we file a bugreport?
http://qc.embarcadero.com/wc/qcmain.aspx
Misses GUID, in the interface, key CTRL + SHIFT + G, as below :
IHasValueR<T> = interface
['{45609E3B-D9A6-40FB-B9E8-86E3FE0D20EF}']
function GetValue: T;
end;
I have a Delphi generic class that exposes a function with an argument of the generic type. Inside this function, I need to pass an instance of the generic type on to another object expecting a Variant type. Similar to this:
type
IMyInterface = interface
DoStuff(Value: Variant);
end;
TMyClass<T> = class
FMyIntf: IMyInterface
procedure DoStuff(SomeValue: T);
end;
[...]
procedure MyClass<T>.DoStuff(SomeValue: T);
begin
FMyIntf.DoStuff((*convert SomeValue to Variant here*));
end;
I tried using Rtti.TValue.From(SomeValue).AsVariant. This worked for integral types, but blew up for Booleans. I don't quite see why, since normally I'd be able to assign a Boolean value to a Variant...
Is there a better way to make this conversion? I only need it to work for simple built-in types (excluding enumerations and records)
I think there is no direct way to convert generic type to variant because variant cannot hold all the possible types. You must write your specific conversion routine. E.g.:
interface
//...
type
TDemo = class
public
class function GetAsVariant<T>(const AValue: T): Variant;
end;
//...
implementation
uses
Rtti,
TypInfo;
//...
{ TDemo}
class function TDemo.GetAsVariant<T>(const AValue: T): Variant;
var
val: TValue;
bRes: Boolean;
begin
val := TValue.From<T>(AValue);
case val.Kind of
tkInteger: Result := val.AsInteger;
tkInt64: Result := val.AsInt64;
tkEnumeration:
begin
if val.TryAsType<Boolean>(bRes) then
Result := bRes
else
Result := val.AsOrdinal;
end;
tkFloat: Result := val.AsExtended;
tkString, tkChar, tkWChar, tkLString, tkWString, tkUString:
Result := val.AsString;
tkVariant: Result := val.AsVariant
else
begin
raise Exception.Create('Unsupported type');
end;
end;
end;
Because TValue.AsVariant handles most of the type conversions internally, this function can be simplified. I will handle enumerations in case you could need them later:
class function TDemo.GetAsVariant<T>(const AValue: T): Variant;
var
val: TValue;
begin
val := TValue.From<T>(AValue);
case val.Kind of
tkEnumeration:
begin
if val.TypeInfo = TypeInfo(Boolean) then
Result := val.AsBoolean
else
Result := val.AsOrdinal;
end
else
begin
Result := val.AsVariant;
end;
end;
Possible usage:
var
vValue: Variant;
begin
vValue := TDemo.GetAsVariant<Boolean>(True);
Assert(vValue = True); //now vValue is a correct Boolean
Looks like in my Delphi version 10.2 the Boolean problem is gone and TValue.From<T>(FValue).AsVariant is enough.
Here an example with some other helpful things like comparing the generic type:
TMyValue<T> = class(TPersistent)
private
FValue: T;
procedure SetValue(const AValue: T);
function GetAsVariant: Variant; override;
public
procedure Assign(Source: TPersistent); override;
property Value: T read FValue write SetValue;
property AsVariant: Variant read GetAsVariant;
end;
function TMyValue<T>.GetAsVariant: Variant;
begin
Result:= TValue.From<T>(FValue).AsVariant;
end;
procedure TMyValue<T>.SetValue(const AValue: T);
begin
if TEqualityComparer<T>.Default.Equals(AValue, FValue) then Exit;
FValue:= AValue;
//do something
end;
procedure TMyValue<T>.Assign(Source: TPersistent);
begin
if Source is TMyValue<T> then Value:= (Source as TMyValue<T>).Value
else inherited;
end;
Another way (tested XE10)
Var
old : variant;
val : TValue;
Begin
val := TValue.FromVariant(old);
End;
I guess this should be an easy one cause I must be doing something wrong.
this is my code, I'm trying to do a Strategy pattern in Delphi:
unit Pattern;
interface
type
TContext = class;
IStrategy = interface
function Move(c: TContext): integer;
end;
TStrategy1 = class(TInterfacedObject, IStrategy)
public
function Move(c: TContext): integer;
end;
TStrategy2 = class(TInterfacedObject, IStrategy)
public
function Move(c: TContext): integer;
end;
TContext = class
const
START = 5;
private
FStrategy: IStrategy;
public
FCounter: integer;
constructor Create;
function Algorithm(): integer;
procedure SwitchStrategy();
end;
implementation
{ TStrategy1 }
function TStrategy1.Move(c: TContext): integer;
begin
c.FCounter := c.FCounter + 1;
Result := c.FCounter;
end;
{ TStrategy2 }
function TStrategy2.Move(c: TContext): integer;
begin
c.FCounter := c.FCounter - 1;
Result := c.FCounter;
end;
{ TContext }
function TContext.Algorithm: integer;
begin
Result := FStrategy.Move(Self)
end;
constructor TContext.Create;
begin
FCounter := 5;
FStrategy := TStrategy1.Create();
end;
procedure TContext.SwitchStrategy;
begin
if FStrategy is TStrategy1 then
FStrategy := TStrategy2.Create()
else
FStrategy := TStrategy1.Create();
end;
end.
And the if FStrategy is TStrategy1 then is giving me: Operator not applicable to this operand type.
What am I doing wrong here cause this should work as I understand from a lot of Delphi language references?
You have omitted the GUID from your interface. is can't work without it.
Edit: On second glance, it still won't work. You can't use is to test an interface reference for its implementing object typein Delphi (well, not directly, anyway). You should change your design. For example, you could either alter the interface or add another interface to return a description of the implementation.
You could make this work by adding the IID/GUID as Craig states, and then changing SwitchStrategy to:
procedure TContext.SwitchStrategy;
begin
if (FStrategy as TObject) is TStrategy1 then
FStrategy := TStrategy2.Create()
else
FStrategy := TStrategy1.Create();
end;
This only works with more modern versions of Delphi. I think Delphi 2010 was where the ability to cast an interface to its implementing object was added.
However, I'd be inclined to avoid this solution and go for something like this:
type
IStrategy = interface
function Move(c: TContext): integer;
function Switch: IStrategy;
end;
TStrategy1 = class(TInterfacedObject, IStrategy)
public
function Move(c: TContext): integer;
function Switch: IStrategy;
end;
TStrategy2 = class(TInterfacedObject, IStrategy)
public
function Move(c: TContext): integer;
function Switch: IStrategy;
end;
function TStrategy1.Switch: IStrategy;
begin
Result := TStrategy2.Create;
end;
function TStrategy2.Switch: IStrategy;
begin
Result := TStrategy1.Create;
end;
procedure TContext.SwitchStrategy;
begin
FStrategy := FStrategy.Switch;
end;
When you find yourself asking an object what type it is, that's usually indicative of a design weakness.
I'm checking out the Delphi 2009 Trial, but run into problems with the generics stuff right away.
The following code does not compile, and I haven't the slightest idea why it's giving me E2015 for the Equals() method:
type
TPrimaryKey<T> = class(TObject)
strict private
fValue: T;
public
constructor Create(AValue: T);
function Equals(Obj: TObject): boolean; override;
function GetValue: T;
end;
constructor TPrimaryKey<T>.Create(AValue: T);
begin
inherited Create;
fValue := AValue;
end;
function TPrimaryKey<T>.Equals(Obj: TObject): boolean;
begin
Result := (Obj <> nil) and (Obj is TPrimaryKey<T>)
and (TPrimaryKey<T>(Obj).GetValue = fValue);
end;
function TPrimaryKey<T>.GetValue: T;
begin
Result := fValue;
end;
Why does the compiler think that fValue and the result of GetValue() can not be compared?
What if T is a string? What if it's a TSize record?
Without constraining T (e.g. with <T :class>), you can't be sure that the comparison will be meaningful.
If, instead, you wanted to compare two values of type T, you can use the Generics.Defaults unit and use:
TEqualityComparer<T>.Default.Equals(x, y)
to compare values x and y of type T.
You can't use operators with untyped generics. See here for a discussion.
It compiles if you change it to:
TPrimaryKey<T: class> = class(TObject)
I think the original poster is trying to create an object wrapper around simple types (Integer, double etc etc), so constraining T to Class would perhaps not work for what he wants.
The compiler has trouble in determining that both "T"'s are the same. But with a little trick you can make it work:
type
TPrimaryKey<T> = class(TObject)
public
type
TCompare<T1> = reference to function(const A1, A2: TPrimaryKey<T1>): Boolean;
private
fValue: T;
fCompare : TCompare<T>;
public
constructor Create(AValue: T; ACompare: TCompare<T>);
function Equals(Obj: TPrimaryKey<T>): Boolean; reintroduce;
function GetValue: T;
function CreateNew(const AValue: T): TPrimaryKey<T>;
end;
constructor TPrimaryKey<T>.Create(AValue: T; ACompare: TCompare<T>);
begin
inherited Create;
fValue := AValue;
fCompare := ACompare;
end;
function TPrimaryKey<T>.Equals(Obj: TPrimaryKey<T>): Boolean;
begin
Result := FCompare(self, Obj);
end;
function TPrimaryKey<T>.GetValue: T;
begin
Result := fValue;
end;
function TPrimaryKey<T>.CreateNew(const AValue: T): TPrimaryKey<T>;
begin
Result := TPrimaryKey<T>.Create(AValue, FCompare);
end;
You instantiate it with:
var
p1, p2 : TPrimaryKey<Integer>;
begin
p1 := TPrimaryKey<Integer>.Create(10,
function(const A1, A2: TPrimaryKey<Integer>): Boolean
begin
Result := (A1<>nil) and (A2<>nil) and (A1.GetValue=A2.GetValue);
end);
p2 := p1.CreateNew(10);
p1.Equals(p2);
end;