Re: Union type casts

From: Date: Wed, 05 Oct 2022 06:28:11 +0000
Subject: Re: Union type casts
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-118746@lists.php.net to get a copy of this message
As I mentioned before, it would work the same alike way as if we called function with such type-hint. FMPOV, one of the most meaningful use-cases would be nullable casts (like casting ?string into ?float for example). Regards, Yevhen On Wed, Oct 5, 2022, 8:41 AM Dusk <dusk@woofle.net> wrote: > 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 (#118746) next »