Skip to content

Instantly share code, notes, and snippets.

@omonien
Last active February 17, 2021 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omonien/24b9b4ba64d46bdfa478615e8e9b0ae6 to your computer and use it in GitHub Desktop.
Save omonien/24b9b4ba64d46bdfa478615e8e9b0ae6 to your computer and use it in GitHub Desktop.
Testing a possible generics related compiler error. UPDATE: It's a feature, not a bug.
program GenericsIssue;
{$APPTYPE CONSOLE}
{$R *.res}
//Simplified test case for https://quality.embarcadero.com/browse/RSP-32427
uses
System.SysUtils, System.Rtti;
type
TFoo = class(TObject)
public
function DoSomething(SomeObject: TValue): string; overload;
function DoSomething<T>(SomeObject: T): string; overload;
end;
TBar = class(TObject)
end;
{ TFoo }
function TFoo.DoSomething(SomeObject: TValue): string;
begin
result := 'NON GENERIC';
end;
function TFoo.DoSomething<T>(SomeObject: T): string;
begin
result := 'GENERIC';
end;
begin
var
Foo := TFoo.Create;
var
Bar := TBar.Create;
try
//Both variants of calling "DoSomething" will be resolved to the generic method.
//This is because type parameters can be omitted, in which case the type is infered.
//As type inferrence takes precedence over overload resolution, the generic implementation get's called in both cases.
Assert(Foo.DoSomething(Bar) = 'GENERIC', 'Non Generic Call');
Assert(Foo.DoSomething<TBar>(Bar) = 'GENERIC', 'Generic Call');
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment