I am migrating a whole lot of classes to Int64 from Integer which in my 32 bit application are 32 bit.
I would like the compiler to throw an exception if I pass an Integer which is 32 bit to a procedure whose type parameter is explictly defined as Int64 but as Int32 is Int64 compatible no error is shown.
I want to do this so I can make sure I don't miss any references.
You could get the desired information by making use of Method Overloading which will make sure that Delphi executes the overloaded method whose input parameter types matches to the passed parameter types.
Then for any of the overloaded methods whose input parameters does not match your desired input parameters you mark such methods as deprecated. This is going to create W1000 Symbol '%s' is deprecated (Delphi) anywhere where such overloaded method is called.
Now the caveat with this approach is that you may need to make 8 overloaded versions of your methods in order to cover four signed integer types (Int8, Int16, Int32 and Int64) as well as four unsigned integer types (UInt8, UInt16, UInt32, UInt64);
Here is a small example of four overloaded methods detecting 4 signed integer types:
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure Test(I: Int8); overload; deprecated;
procedure Test(I: Int16); overload; deprecated;
procedure Test(I: Int32); overload; deprecated;
procedure Test(I: Int64); overload;
public
{ Public declarations }
end;
procedure TForm1.Test(I: ShortInt);
begin
MessageDlg('8 bit integer',mtInformation,[mbOK],0);
end;
procedure TForm1.Test(I: Int16);
begin
MessageDlg('16 bit integer',mtInformation,[mbOK],0);
end;
procedure TForm1.Test(I: Int32);
begin
MessageDlg('32 bit integer',mtInformation,[mbOK],0);
end;
procedure TForm1.Test(I: Int64);
begin
MessageDlg('64 bit integer',mtInformation,[mbOK],0);
end;
procedure TForm1.Button1Click(Sender: TObject);
var ShI: ShortInt;
SmI: SmallInt;
I: Integer;
I64: Int64;
NatI: NativeInt;
begin
Test(ShI);
Test(SmI);
Test(I);
Test(I64);
Test(NatI);
end;
Note that with last test call the returned value for NativeInt depends on whether your application is compiled for 32 bit or 64 bit architecture as is expected.