Union types | true pseudo-type

php.internals

Benjamin Morel

6 years ago
Hi internals, I'd like to bring back the topic of supporting "true" as a type in PHP 8 union types. Now that the RFC <https://wiki.php.net/rfc/union_types_v2> has been successfully voted, I'd like to revive the discussion about adding the "true" pseudo-type in addition to "false". In addition to my Packagist survey <https://externals.io/message/106844#107304>, I'd like to point out that I just came across a native method in the SSH2 extension, that does use true as a type: https://www.php.net/manual/en/function.ssh2-auth-none.php Returns TRUE if the server does accept "none" as an authentication method,
> or an array of accepted authentication methods on failure.
Of course, you could type-hint it array|bool, but the same could be said for |false. Thoughts? — Benjamin

Jordi Boggiano

6 years ago
On 20/01/2020 18:30, Benjamin Morel wrote:
> I'd like to bring back the topic of supporting "true" as a type in PHP 8 > union types. > Now that the RFC <https://wiki.php.net/rfc/union_types_v2> has been > successfully voted, I'd like to revive the discussion about adding the > "true" pseudo-type in addition to "false". > > [..] > > Thoughts? > > — Benjamin
Hey, Looking at my current project's code, I found one place where we use a "string|true" as return type for an authentication method which returns true or an error code if it fails. Of course all the use cases we can argue about whether they are valid and so on, but my main problem is the weird state of having false as a type and not true. It just feels like an inconsistency in the language that'll surely bite people when they'll try to use true as a type. It might not be often, but it's kind of a shame if we can foresee it and prevent it from happening. TL;DR: true Best, Jordi
-- Jordi Boggiano @seldaek - https://seld.be

Nikita Popov

6 years ago
On Mon, Jan 20, 2020 at 6:30 PM Benjamin Morel <benjamin.morel@gmail.com> wrote:
> Hi internals, > > I'd like to bring back the topic of supporting "true" as a type in PHP 8 > union types. > Now that the RFC <https://wiki.php.net/rfc/union_types_v2> has been > successfully voted, I'd like to revive the discussion about adding the > "true" pseudo-type in addition to "false". > > In addition to my Packagist survey > <https://externals.io/message/106844#107304>, I'd like to point out that I > just came across a native method in the SSH2 extension, that does use true > as a type: > > https://www.php.net/manual/en/function.ssh2-auth-none.php > > > Returns TRUE if the server does accept "none" as an authentication method, > > or an array of accepted authentication methods on failure. > > > > Of course, you could type-hint it array|bool, but the same could be said > for |false. > > Thoughts? >
Generally I'm fine with also allowing the "true" type. We certainly have quite a few extension methods that return isolated true values for legacy reasons. However, before "true" is allowed, it is important to answer the two questions at the end of https://wiki.php.net/rfc/union_types_v2#literal_types. Namely, how do true/false interact with weak typing coercions (currently "false" only accepts "false", but not 0 or "") and whether true|false should be equivalent to bool (also related to the previous question). Regards, Nikita

Alexandru Pătrănescu

6 years ago
On Mon, Jan 27, 2020 at 11:43 AM Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Mon, Jan 20, 2020 at 6:30 PM Benjamin Morel <benjamin.morel@gmail.com> > wrote: > > > Hi internals, > > > > I'd like to bring back the topic of supporting "true" as a type in PHP 8 > > union types. > > Now that the RFC <https://wiki.php.net/rfc/union_types_v2> has been > > successfully voted, I'd like to revive the discussion about adding the > > "true" pseudo-type in addition to "false". > > > > In addition to my Packagist survey > > <https://externals.io/message/106844#107304>, I'd like to point out > that I > > just came across a native method in the SSH2 extension, that does use > true > > as a type: > > > > https://www.php.net/manual/en/function.ssh2-auth-none.php > > > > > > Returns TRUE if the server does accept "none" as an authentication > method, > > > or an array of accepted authentication methods on failure. > > > > > > > > Of course, you could type-hint it array|bool, but the same could be said > > for |false. > > > > Thoughts? > > > > Generally I'm fine with also allowing the "true" type. We certainly have > quite a few extension methods that return isolated true values for legacy > reasons. However, before "true" is allowed, it is important to answer the > two questions at the end of > https://wiki.php.net/rfc/union_types_v2#literal_types. > > Namely, how do true/false interact with weak typing coercions (currently > "false" only accepts "false", but not 0 or "") and whether true|false > should be equivalent to bool (also related to the previous question). > > Regards, > Nikita >
Hi, I think we can continue in a similar conservative approach, without coercion for *true*, similar as for *false*. For the cases mentioned so far, we are interested in exactly the true value, not true-ish value like 1. This would make *true|false* not equivalent with *bool* when strict_types is not enabled and equivalent when strict_types is enabled. Alex