# Union types | true pseudo-type

- Thread: https://externals.io/message/108204
- Original archive: https://news-web.php.net/php.internals/108204
- Started by Benjamin Morel on 2020-01-20T17:30:03+00:00
- Messages: 4
- RFC: Union Types v2 (Implemented (PHP 8.0)) — https://externals.io/rfc/union_types_v2.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #108204 — Benjamin Morel — 2020-01-20T17:30:03+00:00

URL: https://externals.io/message/108204#108204

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

---

## #108205 — Jordi Boggiano — 2020-01-21T07:12:05+00:00

URL: https://externals.io/message/108204#108205 · In reply to: #108204

> 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

---

## #108263 — Nikita Popov — 2020-01-27T09:42:52+00:00

URL: https://externals.io/message/108204#108263 · In reply to: #108204

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,
> > […]
>
>
>
> 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

---

## #108281 — drealecs@gmail.com — 2020-01-28T03:46:08+00:00

URL: https://externals.io/message/108204#108281 · In reply to: #108263

> On Mon, Jan 20, 2020 at 6:30 PM Benjamin Morel <benjamin.morel@gmail.com>
> wrote:
>
> > […]
> that I
> > […]
> true
> > […]
> method,
> > […]
>
> 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
