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]

Reply via email to