I always was thinking that OleVariant variables always has initial value equal to Unassigned (type VT_EMPTY). But the following simple code compiled with XE3 shows me it is not true.
{$APPTYPE CONSOLE}
uses
ActiveX;
function GetValue: OleVariant;
begin
Result := TVariantArg(Result).vt;
end;
function GetValue2: OleVariant;
begin
Result := 10;
Result := GetValue;
end;
var
Msg: string;
begin
Msg := GetValue2;
Writeln(Msg);
end.
App writes "3". Is it normal?
The return value of a Delphi function, for types that don't fit in a register, are passed as var parameters. So the compiler transforms the code to be like so:
procedure GetValue(var Result: OleVariant);
Hence the value of Result
on entry to the function is the value of the variable that you assign the return value to.
So your calling code is transformed to
function GetValue2: OleVariant;
begin
Result := 10;
GetValue(Result);
end;
So in its entirety your program becomes
{$APPTYPE CONSOLE}
uses
ActiveX;
procedure GetValue(var Result: OleVariant);
begin
Result := TVariantArg(Result).vt;
end;
procedure GetValue2(var Result: OleVariant);
begin
Result := 10;
GetValue(Result);
end;
var
tmp: OleVariant;
Msg: string;
begin
GetValue2(tmp);
Msg := tmp;
Writeln(Msg);
end.
Which explains the output of VT_I4
.
Of course this is all a consequence of implementation detail. You should always initialize function return values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With