Re: Union type casts

From: Date: Wed, 05 Oct 2022 05:41:48 +0000
Subject: Re: Union type casts
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-118745@lists.php.net to get a copy of this message
On Oct 4, 2022, at 21:46, Eugene Sidelnyk <zsidelnik@gmail.com> wrote: > $foo = (int|float)$bar; As written, I wouldn't know what to expect this to do with a string value -- would it cast it to int or float? Based on the behavior of your second example, the answer appears to be "float", so this syntax seems to be equivalent to: $foo = \is_int($bar) ? $bar : (float) $bar; Or, even more concisely: $foo = 0+$bar; I'd be even less sure what to expect when casting to other union types. What would the expected result of casting a string to (bool|array|object) be, for example? I'm not sure there are many meaningful operations which could be constructed here.

« previous php.internals (#118745) next »