SysUtils.Format
関数と値を使用していvariant
ますが、この関数は書式文字列が%s
. 関数に関するドキュメントを確認しましたFormat
が、バリアント値がどのように扱われるかについての参照はありません。
この単純なアプリケーションを考えてみましょう:
{$APPTYPE CONSOLE}
uses
Variants,
SysUtils;
procedure TestFormat;
var
v : Variant;
begin
v:=100;
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %s',[v]));//ok
v:='100';
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %s',[v]));//ok
v:=100;
writeln(Format('The VarType of v is %s',[VarTypeAsText(VarType(v))]));
writeln(Format('The value of v is %d',[v]));//raise a EConvertError exception EConvertError: Format '%d' invalid or incompatible with argument
end;
begin
try
TestFormat;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
readln;
end.
これはバグですか、それともこの機能の単純な制限ですか?
Delphi 5、Delphi 2007、および Delphi XE でこの動作を確認しました。