Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118747 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 14818 invoked from network); 5 Oct 2022 06:35:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Oct 2022 06:35:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 816F11804AA for ; Tue, 4 Oct 2022 23:34:59 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS17378 206.123.64.0/18 X-Spam-Virus: No X-Envelope-From: Received: from mail1.25mail.st (mail1.25mail.st [206.123.115.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 4 Oct 2022 23:34:59 -0700 (PDT) Received: from smtpclient.apple (unknown [49.48.240.255]) by mail1.25mail.st (Postfix) with ESMTPSA id E60CD60542; Wed, 5 Oct 2022 06:34:49 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) In-Reply-To: <6F41F6B1-E27B-4BF4-BDB0-66D63DA4F130@woofle.net> Date: Wed, 5 Oct 2022 13:34:43 +0700 Cc: Eugene Sidelnyk , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <6F41F6B1-E27B-4BF4-BDB0-66D63DA4F130@woofle.net> To: Dusk X-Mailer: Apple Mail (2.3696.120.41.1.1) Subject: Re: [PHP-DEV] Union type casts From: php-lists@koalephant.com (Stephen Reay) > On 5 Oct 2022, at 12:41, Dusk wrote: >=20 > On Oct 4, 2022, at 21:46, Eugene Sidelnyk wrote: >> $foo =3D (int|float)$bar; >=20 > As written, I wouldn't know what to expect this to do with a string = value -- would it cast it to int or float? >=20 > Based on the behavior of your second example, the answer appears to be = "float", so this syntax seems to be equivalent to: >=20 > $foo =3D \is_int($bar) ? $bar : (float) $bar; >=20 > Or, even more concisely: >=20 > $foo =3D 0+$bar; >=20 > 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. > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20 Hi, I think the intention is that it would follow precisely the same casting = rules as function parameters do. So e.g: ``` function foo(int|float $var) { return $var;=20 } $f1 =3D foo(=E2=80=981=E2=80=99); $c1 =3D (int|float) =E2=80=981=E2=80=99; $f2 =3D foo(=E2=80=981.1=E2=80=99); $c2 =3D (int|float) =E2=80=981.1=E2=80=99; ``` In both scenarios the $f1/$c1 and $f2/$c2 would result in the same type. Your first example isn=E2=80=99t quite the same though; Passing =E2=80=981= =E2=80=99 to a parameter typed as `int|float` will cast it to an = integer, your example casts such a value to a float. The `0+$value` thing is interesting, but also definitely seems less = intuitive to my eye. Given your last comment I think its worth reiterating Eugene=E2=80=99s = point is simply about exposing the **existing** cast behaviour that = happens with typed parameters, to be usable on variables. I can see some benefit in the proposal, but I think I=E2=80=99d be more = interested in the ability to define local variables as being a given = type (or union), and then have the engine cast (or error) when assigning = to them - essentially how typed object properties work, but for regular = variables.