Re: Proposal: AS assertions

From: Date: Fri, 22 Mar 2024 08:17:47 +0000
Subject: Re: Proposal: AS assertions
References: 1 2 3 4 5 6 7 8  Groups: php.internals 
Request: Send a blank email to internals+get-122722@lists.php.net to get a copy of this message
On 2024-03-21 16:02, Robert Landers wrote:
$a as int|float would be an int, float, or thrown exception. $a as int|float|null would be an int, float, or null.
Just a suggestion here which might be more palatable to Rowan's wish for consistency (which I can totally relate to): We perhaps could make sure that as does not throw if used with ??, or that ?? catches the type error and returns the right-hand expression instead: So to do a nullable typecast you would do:
    $a as int|float ?? null
To me this reads way more intuitive what will happen, and achieves the same in an also very concise way. The only catch I see is that it would also swallow errors about $a not being defined at all. Best, Jordi -- Jordi Boggiano @seldaek -https://seld.be

« previous php.internals (#122722) next »