Re: Union type casts
| From: | Eugene Sidelnyk | Date: | Wed, 05 Oct 2022 17:53:11 +0000 |
| Subject: | Re: Union type casts | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118757@lists.php.net to get a copy of this message | ||
I used to have an awkward feeling when it was necessary to convert the
nullable integer into a nullable string. It would be kind of ternary
operator anyway. Either
$b = (null !== $a) ? (string)$a : null; or `$b =
(string)$a ?: null;`.
A lot easier to read would it be if written this way: $b = (?string)$a;.
The intentions are clear when doing explicit type-cast.
Currently, I don't see any other situations when this feature may be useful
Thank you
On Wed, Oct 5, 2022, 4:43 PM juan carlos morales <dev.juan.morales@gmail.com>
wrote:
> There is a difference is declaring a function parameter of different
> types ... because before hand, when the code calls the function, we
> might not be sure about what kind if data the function will receive,
> so the function could expect different types, and work the parameter
> correctly internally..... nice
>
>
> but ... when casting, on purpose, what use case can apply to it?
>
> Are you facing a situation where manual union type cast is needed? can
> you share it with us?
>
> thanks
>