Testing the type of a generic in delphi

4k views Asked by At

I want some way to write a function in delphi like the following

procedure Foo<T>;
begin
    if T = String then
    begin
        //Do something
    end;

    if T = Double then
    begin
        //Do something else
    end;
end;

ie: I want to be able to do different things based on a generic type

I've tried using TypeInfo in System but this seems to be suited to objects rather than generic types.

I'm not even sure this is possible in pascal

2

There are 2 answers

0
Ondrej Kelle On BEST ANSWER

TypeInfo should work:

type
  TTest = class
    class procedure Foo<T>;
  end;

class procedure TTest.Foo<T>;
begin
  if TypeInfo(T) = TypeInfo(string) then
    Writeln('string')
  else if TypeInfo(T) = TypeInfo(Double) then
    Writeln('Double')
  else
    Writeln(PTypeInfo(TypeInfo(T))^.Name);
end;

procedure Main;
begin
  TTest.Foo<string>;
  TTest.Foo<Double>;
  TTest.Foo<Single>;
end;
2
David Heffernan On

From XE7 onwards you can use GetTypeKind to find the type kind:

case GetTypeKind(T) of
tkUString:
  ....
tkFloat:
  ....
....
end;

Of course tkFloat identifies all floating point types so you might also test SizeOf(T) = SizeOf(double).

Older versions of Delphi do not have the GetTypeKind intrinsic and you have to use PTypeInfo(TypeInfo(T)).Kind instead. The advantage of GetTypeKind is that the compiler is able to evaluate it and optimise away branches that can be proven not to be selected.

All of this rather defeats the purpose of generics though and one wonders if your problem has a better solution.