Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TTest<T> = class
- private
- f : T;
- public
- function ToString : string;
- end;
- TTest<T>.ToString;
- begin
- Result := f.ToString;
- end;
- TTest<T: TObject>.ToString;
- begin
- Result := T.ToString;
- end;
- type
- TToString<T> = reference to function(const AValue: T): string;
- TGenContainer<T> = class
- private
- FValue: T;
- FToString : TToString<T>;
- public
- constructor Create(const AToString: TToString<T>);
- function ToString: string;
- property Value: T read FValue write FValue;
- end;
- constructor TGenContainer<T>.Create(const AToString: TToString<T>);
- begin
- FToString := AToString;
- end;
- function TGenContainer<T>.ToString: string;
- begin
- Result := FToString(FValue);
- end;
- procedure TForm2.Button1Click(Sender: TObject);
- var
- gen : TGenContainer<Integer>;
- begin
- gen := TGenContainer<Integer>.Create(
- function(const AValue: Integer): string
- begin
- Result := IntToStr(AValue);
- end);
- try
- gen.Value := 17;
- Memo1.Lines.Add(gen.ToString);
- finally
- gen.Free;
- end;
- end;
Add Comment
Please, Sign In to add comment