Re: Proposal: AS assertions

From: Date: Thu, 21 Mar 2024 16:30:15 +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-122711@lists.php.net to get a copy of this message
On 21/03/2024 15:02, Robert Landers wrote:
I don't think you are getting what I am saying. $a as int|float would be an int, float, or thrown exception. $a as int|float|null would be an int, float, or null.
I get what you're saying, but I disagree that it's a good idea. If $a is 'hello', both of those statements should throw exactly the same error, for exactly the same reason - the input is not compatible with the type you have specified.
Another way of thinking about is: $x = $a as null What do you expect $x to be?
The same as $x inside this function: function foo(null $x) { var_dump($x); } foo($a); Which is null if $a is null, and a TypeError if $a is anything else: https://3v4l.org/5UR5A Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#122711) next »