feat(std): make std::any function accept optional parameter with null default

- Updated std::any to accept zero or one arguments instead of requiring exactly one
- When no argument is provided, std::any now returns null by default
- Modified parser logic to handle omitted parameter case in FunctionCallTrait
- Updated documentation in both English and Chinese to reflect optional parameter syntax
- Added test case demonstrating std::any() call without parameters returning null
- Changed error message to indicate zero or one argument requirement instead of exactly one
master
韩天峰 1 month ago
parent 692841a60b
commit 12b0781914
  1. 2
      docs/en/COMPILE_TIME_FUNCTIONS.md
  2. 2
      docs/zh-cn/COMPILE_TIME_FUNCTIONS.md
  3. 8
      src/Parser/FunctionCallTrait.php
  4. 7
      tests/compiler/basic/std-any.phpt

@ -50,7 +50,7 @@ There are currently 14 `std::` compile-time entry points.
| `std::bigInt($value)` | Constructs a BigInt. | Implicit construction from a float variable is not allowed. | | `std::bigInt($value)` | Constructs a BigInt. | Implicit construction from a float variable is not allowed. |
| `std::decimal($value)` | Constructs a Decimal. | A float variable must be converted via string or integer; float literals are handled per the original literal. | | `std::decimal($value)` | Constructs a Decimal. | A float variable must be converted via string or integer; float literals are handled per the original literal. |
| `std::bigFloat($value)` | Constructs a BigFloat. | Requires 1 value parameter. | | `std::bigFloat($value)` | Constructs a BigFloat. | Requires 1 value parameter. |
| `std::any($value)` | Degrades the expression to `mixed/any`. | Native objects and native-object std containers cannot escape through it. | | `std::any([$value])` | Degrades the expression to `mixed/any`; when omitted, the value defaults to `null`. | Native objects and native-object std containers cannot escape through it. |
| `std::ref($target)` | Explicitly passes a target by reference. | Only accepts variables, array elements, or object properties and is only valid as a call argument wrapper. | | `std::ref($target)` | Explicitly passes a target by reference. | Only accepts variables, array elements, or object properties and is only valid as a call argument wrapper. |
| `std::expected($condition)` | Marks a condition as usually true. | Accepts exactly one non-unpacked argument and returns bool. | | `std::expected($condition)` | Marks a condition as usually true. | Accepts exactly one non-unpacked argument and returns bool. |
| `std::unexpected($condition)` | Marks a condition as usually false. | Accepts exactly one non-unpacked argument and returns bool. | | `std::unexpected($condition)` | Marks a condition as usually false. | Accepts exactly one non-unpacked argument and returns bool. |

@ -48,7 +48,7 @@ TypePHP 不再为编译器指令保留任何全局函数名。编译期 API 最
| `std::bigInt($value)` | 构造 BigInt。 | 不允许从 float 变量隐式构造。 | | `std::bigInt($value)` | 构造 BigInt。 | 不允许从 float 变量隐式构造。 |
| `std::decimal($value)` | 构造 Decimal。 | float 变量需改用字符串或整型;float 字面量会按原始字面量处理。 | | `std::decimal($value)` | 构造 Decimal。 | float 变量需改用字符串或整型;float 字面量会按原始字面量处理。 |
| `std::bigFloat($value)` | 构造 BigFloat。 | 需要 1 个值参数。 | | `std::bigFloat($value)` | 构造 BigFloat。 | 需要 1 个值参数。 |
| `std::any($value)` | 将表达式降级为 `mixed/any`。 | Native 对象及包含 Native 对象的 std 容器不能通过它逃逸。 | | `std::any([$value])` | 将表达式降级为 `mixed/any`;省略参数时,初始值为 `null`。 | Native 对象及包含 Native 对象的 std 容器不能通过它逃逸。 |
| `std::ref($target)` | 显式以引用方式传递目标。 | 只接受变量、数组元素或对象属性,且仅可作为调用参数的引用包装器。 | | `std::ref($target)` | 显式以引用方式传递目标。 | 只接受变量、数组元素或对象属性,且仅可作为调用参数的引用包装器。 |
| `std::expected($condition)` | 标记条件通常为真。 | 只接受一个非展开参数并返回 bool。 | | `std::expected($condition)` | 标记条件通常为真。 | 只接受一个非展开参数并返回 bool。 |
| `std::unexpected($condition)` | 标记条件通常为假。 | 只接受一个非展开参数并返回 bool。 | | `std::unexpected($condition)` | 标记条件通常为假。 | 只接受一个非展开参数并返回 bool。 |

@ -217,15 +217,19 @@ trait FunctionCallTrait
} }
/** /**
* Erase the static type of a value through std::any(). * Erase the static type of a value through std::any(). An omitted value
* explicitly creates dynamic storage initialized to null.
*/ */
protected function parseAnyCompileTimeCall(CallLike $expr): string protected function parseAnyCompileTimeCall(CallLike $expr): string
{ {
if (count($expr->args) === 0) {
return self::VALUE_NULL;
}
if (count($expr->args) !== 1 if (count($expr->args) !== 1
|| !$expr->args[0] instanceof Node\Arg || !$expr->args[0] instanceof Node\Arg
|| $expr->args[0]->unpack || $expr->args[0]->unpack
) { ) {
$this->fatalError($expr, 'The std::any function expects exactly one non-unpacked argument'); $this->fatalError($expr, 'The std::any function expects zero or one non-unpacked argument');
} }
$value = $expr->args[0]->value; $value = $expr->args[0]->value;
if ($this->isNativeObjectClass($this->detectClassOfExpr($value))) { if ($this->isNativeObjectClass($this->detectClassOfExpr($value))) {

@ -4,10 +4,17 @@ any
<?php <?php
function main() function main()
{ {
$ref = std::any();
var_dump($ref);
$ref = 'initialized';
var_dump($ref);
$a = std::any(10); $a = std::any(10);
$b = std::any(4); $b = std::any(4);
echo var_dump($a/$b); echo var_dump($a/$b);
} }
?> ?>
--EXPECT-- --EXPECT--
NULL
string(11) "initialized"
float(2.5) float(2.5)

Loading…
Cancel
Save