use*_*813 4 delphi string formatting
Here is a piece of Delphi code that the compiler perfectly compiles :
Var S: String;
Begin
S := Format('%s %s', ['Hello']);
....
Run Code Online (Sandbox Code Playgroud)
But on execution it raises an exception, of course there are two argument in the left string, and only one in the brackets...
Is there a way to have this error checked by the compiler and to be displayed at least as a warning, or better fatal error?
目前编写的编译器不费力气就知道第一个参数中的引号之间嵌入了什么。它还不计算第二个参数中传递的值,也不知道它们是否正确。在调用函数之前,不会对它们进行求值,因为它们不正确,因此会引发异常。IOW,编译器不会尝试执行任何允许其发出此类警告或错误的操作。
在运行时调用该函数时,它将验证参数的数量,它们是否是正确的类型以及是否提供了正确的参数。编译器不会尝试以任何方式执行此操作。它仅确保为第一个参数提供一个字符串,为第二个参数提供一个const数组。
因此,要直接回答您所问的问题:不,没有办法让编译器检查该错误以发出警告,因为编译器不努力检查允许它执行的任何操作。
| 归档时间: |
|
| 查看次数: |
195 次 |
| 最近记录: |