Re: [RFC] Nullable and non-nullable cast operators

From: Date: Fri, 24 Oct 2025 09:21:58 +0000
Subject: Re: [RFC] Nullable and non-nullable cast operators
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-128947@lists.php.net to get a copy of this message
On Fri, Oct 24, 2025 at 9:09 AM Alexandre Daubois <
alex.daubois+php@gmail.com> wrote:

> Hi everyone,
>
> Nicolas Grekas and I would like to propose this new RFC to the
> discussion. This proposes to introduce two new cast operators to the
> language: (?type) and (!type).
>
> Please find the details here:
> https://wiki.php.net/rfc/nullable-not-nullable-cast-operator
>
> Thanks!
>
> — Alexandre Daubois
>

This looks useful to me
 - In the RFC the "Behavior Comparison Table" the colors make it hard to
read what's listed in the table, could this be adjusted?
 - We still end up having null coalescing operators (or isset) for arrays,
would something like this work?
```
// suggested through RFC
$quantity = ((?int) ($_POST['quantity'] ?? null)) ?? 0;

// the underlying logic would be
$quantity = $_POST['quantity'] ?? null;
if ($quantity !== null && ! is_valid_int_value($quantity)) {
    throw new TypeError();
}
$quantity = (int) $quantity;

// would be nice if it incorporated an "isset check" as it's effectively
treated as a null value
$quantity = ((?int) $_POST['quantity']) ?? 0;
```


Thread (18 messages)

« previous php.internals (#128947) next »