Skip to content

Instantly share code, notes, and snippets.

@jpluimers
Created April 9, 2018 13:44
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 jpluimers/aa271dee237142a925d43c3cc3404eec to your computer and use it in GitHub Desktop.
Save jpluimers/aa271dee237142a925d43c3cc3404eec to your computer and use it in GitHub Desktop.
Delphi does some simple type inference

A quick look into the generated assembler code proves that the type is indeed resolved correctly:

Project17.dpr.26: TGeneric.GuessTheType<Exception>(x);
0041C530 8B151C484200     mov edx,[$0042481c]
0041C536 A1F4974100       mov eax,[$004197f4]
0041C53B E84CD3FFFF       call TGeneric.GuessTheType<System.SysUtils.Exception>
Project17.dpr.27: TGeneric.GuessTheType(x);
0041C540 8B151C484200     mov edx,[$0042481c]
0041C546 A1F4974100       mov eax,[$004197f4]
0041C54B E83CD3FFFF       call TGeneric.GuessTheType<System.SysUtils.Exception>
Project17.dpr.28: TGeneric.GuessTheType(s);
0041C550 8B1520484200     mov edx,[$00424820]
0041C556 A1F4974100       mov eax,[$004197f4]
0041C55B E83CD3FFFF       call TGeneric.GuessTheType<System.SysUtils.TSimpleRWSync>

Source: https://plus.google.com/+Primo%C5%BEGabrijel%C4%8Di%C4%8D/posts/edE3YWtwY3B

program Project17;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
TGeneric = class
class procedure GuessTheType<T>(const value: T);
end;
var
x: Exception;
s: TSimpleRWSync;
{ TGeneric<T> }
class procedure TGeneric.GuessTheType<T>(const value: T);
begin
end;
begin
TGeneric.GuessTheType<Exception>(x);
TGeneric.GuessTheType(x);
TGeneric.GuessTheType(s);
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment