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

Reply via email to