Re: Renaming "strict types" to "scalar type coercion"
| From: | Pierre | Date: | Fri, 21 Jun 2024 12:46:53 +0000 |
| Subject: | Re: Renaming "strict types" to "scalar type coercion" | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123727@lists.php.net to get a copy of this message | ||
Le 21/06/2024 à 14:27, Robert Landers a écrit :
This is why I wanted to work on "as" part of the pattern matching. It isn't clear what will happen with the actual pattern matching RFC (yet), but being able to do: some_function_expecting_int($_GET['foo'] as ?int);And how about: some_function_expecting_int(\intval($_GET['foo'])); And moreover, I'd write something like this, but: function validate_int(mixed $value): int {
if (null === $value || '' === $value) {
return null;
}
if (\is_int($value)) {
return $value;
}
if (\is_string($value) && \ctype_digit($value)) {
return \intval($value);
}
throw new \InvalidArgumentException("What what!");
}
some_function_expecting_int(validate_int($_GET['foo'] ?? null));
But the example might be erroneous, I see your point, nevertheless making coercion explicit doesn't seem really relevant to me, the one point I like in your syntax would be null handling.
--
Pierre