Testing the type of a generic in delphi
Asked Answered
F

2

14

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

Flyweight answered 25/6, 2015 at 6:40 Comment(2)
GetTypeKindshould do this. See delphisorcery.blogspot.de/2014/10/….Postdiluvian
You can use variants?Killer
U
11

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;
Unvalued answered 25/6, 2015 at 7:2 Comment(0)
Z
14

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.

Zacharia answered 25/6, 2015 at 7:0 Comment(2)
FWIW I linked to GetTypeKind doc even though it's not documented in the hope that one day the Emba docs will catch up.Zacharia
Note that if you want to test for T is double then if TypeInfo(T) = TypeInfo(double) then ... will also resolve to an intrinsic and not generate any code.Blondy
U
11

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;
Unvalued answered 25/6, 2015 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.