Consider
type
Use = record
public
class function When<T>(const aBool: Boolean; const WhenTrue: T; const WhenFalse: T): T; static; inline;
end;
{ Use }
class function Use.When<T>(const aBool: Boolean; const WhenTrue, WhenFalse: T): T;
begin
if aBool
then Result := WhenTrue
else Result := WhenFalse
end;
procedure Test(Cond: Boolean);
type
TObjectClass = class of TObject;
var
i: Integer;
b: Byte;
c: Cardinal;
w: Word;
s: String;
d: Double;
o: TObjectClass;
begin
s := Use.When(Cond,'True', 'False');
s := Use.When(Cond, 1, 2).ToString;
i := Use.When<Integer>(Cond, 1, -2);
b := Use.When<Byte>(Cond, 1, 2);
c := Use.When(Cond, 1, 2);
w := Use.When(Cond, 1, 2);
d := Use.When(Cond, 3.14, 42.0);
o := Use<TObjectClass>.When(Cond, TObject, TStringList);
end;
This is valid code.
But
i := Use.When<Integer>(Cond, 1, -2);
requires the type to be specified.
Also
w := Use.When(Cond, 1, 128);
stops working - actually for any signed or unsigned type - when the second parameter is changed to 128 or higher.
It then complains:
Why? I though the left hand type would assist in the type inference and hence the parameter validation?
Edit: I just noticed that this only happens for Error Insight - not during compilation.