# [RFC] Mixed type

- Thread: https://externals.io/message/109715
- Original archive: https://news-web.php.net/php.internals/109715
- Started by Dan Ackroyd on 2020-04-20T11:17:46+00:00
- Messages: 33
- RFC: Mixed type v2 (Implemented (PHP 8.0)) — https://externals.io/rfc/mixed_type_v2.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/109715.md?page=2
- 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

---

## #109715 — Dan Ackroyd — 2020-04-20T11:17:46+00:00

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

Hi Internals,

Here is an RFC for adding a 'mixed' type to the language:
https://wiki.php.net/rfc/mixed_type_v2
The RFC builds on an earlier draft by Michael Moravec.

Part of the motivation for pursuing this RFC was my recent experience
in upgrading an application to PHP 7.4.

Being able to add types to the application made the code much easier
to reason about, but there were places where I couldn't add type info
due to the lack of mixed. That made it hard to see if that piece of
code had been upgraded (but type info couldn't be added), or if that
code still needed to be upgraded.

cheers
Dan
Ack

---

## #109718 — Larry Garfield — 2020-04-20T13:51:51+00:00

URL: https://externals.io/message/109715#109718 · In reply to: #109715

> Hi Internals,
> 
> Here is an RFC for adding a 'mixed' type to the language:
> https://wiki.php.net/rfc/mixed_type_v2
> The RFC builds on an earlier draft by Michael Moravec.
> 
> Part of the motivation for pursuing this RFC was my recent experience
> in upgrading an application to PHP 7.4.
> 
> Being able to add types to the application made the code much easier
> to reason about, but there were places where I couldn't add type info
> due to the lack of mixed. That made it hard to see if that piece of
> code had been upgraded (but type info couldn't be added), or if that
> code still needed to be upgraded.
> 
> cheers
> Dan
> Ack

I am not against this, but now that we have Union types what places are there where the currently available type declarations are insufficient?  Resource seems like the only remaining gap where you'd be forced to use `mixed` instead of a union.

--Larry Garfield

---

## #109719 — Sara Golemon — 2020-04-20T14:15:24+00:00

URL: https://externals.io/message/109715#109719 · In reply to: #109718

On Mon, Apr 20, 2020 at 8:52 AM Larry Garfield <larry@garfieldtech.com>
wrote:

> 
> > […]
>
> I am not against this, but now that we have Union types what places are
> there where the currently available type declarations are insufficient?
> Resource seems like the only remaining gap where you'd be forced to use
> `mixed` instead of a union.
>
> I imagine some type combinations get pretty wide. Like, this is verbose AF.

null|bool|int|float|string|array|object|resource

For the long term good of the language I'd prefer type aliases (or typedefs
or usings or whatever you want to call them.

use mixed = null|bool|int|float|string|array|object|resource;
use scalar = null|bool|int|float|string;
use number = int|float;

That said, baking 'mixed' in as an implicit alias of the above isn't
problematic for that future.

-Sara

---

## #109720 — Larry Garfield — 2020-04-20T15:15:40+00:00

URL: https://externals.io/message/109715#109720 · In reply to: #109719

> On Mon, Apr 20, 2020 at 8:52 AM Larry Garfield <larry@garfieldtech.com>
> wrote:
> 
> > […]
> 
> null|bool|int|float|string|array|object|resource

Sure, but how often is that an actual description of what the code accepts?  I... cannot actually envision what code would actually accept that mess. :-)

With union types and stringable already on the way, I'm not sure what other non-hypothetical use cases would still be that fugly that you'd now need to fall back to `mixed`.  (I still think intersection types are needed, but that's a separate matter.)  There may be, but I cannot think of them.

> For the long term good of the language I'd prefer type aliases (or typedefs
> or usings or whatever you want to call them.
> 
> use mixed = null|bool|int|float|string|array|object|resource;
> use scalar = null|bool|int|float|string;
> use number = int|float;

Concur.

> That said, baking 'mixed' in as an implicit alias of the above isn't
> problematic for that future.
> 
> -Sara

Concur.

--Larry Garfield

---

## #109721 — Benjamin Eberlei — 2020-04-20T15:45:48+00:00

URL: https://externals.io/message/109715#109721 · In reply to: #109720

On Mon, Apr 20, 2020 at 5:17 PM Larry Garfield <larry@garfieldtech.com>
wrote:

> 
> > […]
> verbose AF.
> > […]
>
> Sure, but how often is that an actual description of what the code
> accepts?  I... cannot actually envision what code would actually accept
> that mess. :-)
>

I can think of quite a few:

- var_dump, print_r, more generally debugging/logging functions and
wrappers thereof
- json_encode, serialize, and more generally serialization and encoding
functions, as return value as well for the inverse operations
- database/nosql related functions (bindParam, bindValue) and as return
value from generic functions like fetch()
- Xpath::evaluate
- ldap query functions

And wrappers of these extensions could benefit from passing on the mixed
type explicitly.


> With union types and stringable already on the way, I'm not sure what
> other non-hypothetical use cases would still be that fugly that you'd now
> need to fall back to `mixed`.  (I still think intersection types are
> needed, but that's a separate matter.)  There may be, but I cannot think of
> them.
>
> > […]
> typedefs
> > […]
>
> Concur.
>
> > […]
>
> Concur.
>
> --Larry Garfield

---

## #109722 — kocsismate90@gmail.com — 2020-04-20T15:46:37+00:00

URL: https://externals.io/message/109715#109722 · In reply to: #109720

Hi Larry,


> Sure, but how often is that an actual description of what the code
> accepts?  I... cannot actually envision what code would actually accept
> that mess. :-)
>

For example the recently added `get_debug_type()` function does, along with
dozens of internal functions. Besides messy code, I'm sure that there are
legitimate use-cases for the mixed type in user-land as well.

use mixed = null|bool|int|float|string|array|object|resource;
> use scalar = null|bool|int|float|string;
> use number = int|float;


I'm really not a fan of type aliases (at least user-land ones), but I also
agree with Sara that these 3 "global" aliases would come handy. Actually, I
was considering to also propose "scalar" and "number" in a later RFC.

Cheers,
Máté

---

## #109724 — Markus Fischer — 2020-04-20T15:58:26+00:00

URL: https://externals.io/message/109715#109724 · In reply to: #109721

Hi,


just picking on one of the examples:


> - json_encode, serialize, and more generally serialization and encoding
> functions, as return value as well for the inverse operations

But how would this work for serializing resources, which is part of mixed?

Seems to me exactly this case would benefit from a, albeit long though 
more accurate, actual union type.

Hope that makes sense,
- Markus

---

## #109723 — Guilliam Xavier — 2020-04-20T15:59:02+00:00

URL: https://externals.io/message/109715#109723 · In reply to: #109719

>
> For the long term good of the language I'd prefer type aliases (or typedefs
> or usings or whatever you want to call them.
>
> use mixed = null|bool|int|float|string|array|object|resource;
> use scalar = null|bool|int|float|string;
> use number = int|float;
>
> That said, baking 'mixed' in as an implicit alias of the above isn't
> problematic for that future.

The `scalar` example should be `bool|int|float|string`, without `null`.
And for `mixed`, as already said, we cannot use `resource` in union types.

All the more reason to standardize those "aliases" in core?

--
Guilliam Xavier

---

## #109725 — Guilliam Xavier — 2020-04-20T16:40:57+00:00

URL: https://externals.io/message/109715#109725 · In reply to: #109720

>
> With union types and stringable already on the way, I'm not sure what other non-hypothetical use cases would still be that fugly that you'd now need to fall back to `mixed`.

For instance:
  - the parameter of [a wrapper around] `gettype()` or an is_* function
  - the return of [a generic implementation of]
ArrayAccess::offsetGet() or Iterator::current(), or of a userland
array_value_first() or array_value_last() function

To me, especially the return type declaration can be useful to ensure
that [every code path of] the implementation returns a value.

-- 
Guilliam Xavier

---

## #109726 — kocsismate90@gmail.com — 2020-04-20T16:53:00+00:00

URL: https://externals.io/message/109715#109726 · In reply to: #109724

>
> But how would this work for serializing resources, which is part of mixed?
>

https://3v4l.org/n9D2S

The point is that many internal functions accept all types (including
resources),
even though they might not do much useful with some of them.

Besides the functions that Benjamin mentioned, I'd add the *printf family
that accept "mixed" parameters to the list.

Seems to me exactly this case would benefit from a, albeit long though
> more accurate, actual union type.


Regarding accuracy, I believe the more types you have in an union type, the
less
important exact accuracy is, at least for return types and properties. For
example
if a function has a return type of mixed, I don't consider it a big deal if
the implementation
doesn't actually return 1 type (e.g. resource, null) in practice.

Máté

---

## #109727 — Albert Casademont — 2020-04-20T17:01:37+00:00

URL: https://externals.io/message/109715#109727 · In reply to: #109725

As Larry was asking for an example.

The ParameterBag::get() function from Symfony (and others) that allows you
to fetch request params, for example. That's a mixed type that could return
different values, depending on your request body. Yes, I can type for all
possible scalar values + null + array, but that's a bit of a PITA and
phpstan would complain nonetheless, as the return type of theParameterBag
::get() function is "mixed" and we can't change that. Right now we're
annotating "mixed" with a docblock.

So yes, a "mixed" type is greatly appreciated when dealing with user input.

Thanks!

On Mon, Apr 20, 2020 at 6:41 PM Guilliam Xavier <guilliam.xavier@gmail.com>
wrote:

> On Mon, Apr 20, 2020 at 5:17 PM Larry Garfield <larry@garfieldtech.com>
> wrote:
> > […]
> other non-hypothetical use cases would still be that fugly that you'd now
> need to fall back to `mixed`.
>
> For instance:
>   - the parameter of [a wrapper around] `gettype()` or an is_* function
>   - the return of [a generic implementation of]
> ArrayAccess::offsetGet() or Iterator::current(), or of a userland
> array_value_first() or array_value_last() function
>
> To me, especially the return type declaration can be useful to ensure
> that [every code path of] the implementation returns a value.
>
> --
> Guilliam Xavier

---

## #109733 — Larry Garfield — 2020-04-21T00:34:19+00:00

URL: https://externals.io/message/109715#109733 · In reply to: #109721

> > […]
> 
> I can think of quite a few:
> 
> - var_dump, print_r, more generally debugging/logging functions and
> wrappers thereof
> - json_encode, serialize, and more generally serialization and encoding
> functions, as return value as well for the inverse operations
> - database/nosql related functions (bindParam, bindValue) and as return
> value from generic functions like fetch()
> - Xpath::evaluate
> - ldap query functions
> 
> And wrappers of these extensions could benefit from passing on the mixed
> type explicitly.

Valid examples.  It would be helpful to add those to the RFC for clarity.

--Larry Garfield

---

## #109745 — come.chilliet@fusiondirectory.org — 2020-04-21T09:09:54+00:00

URL: https://externals.io/message/109715#109745 · In reply to: #109719

Le lundi 20 avril 2020, 09:15:24 CEST Sara Golemon a écrit :
> use mixed = null|bool|int|float|string|array|object|resource;
> use scalar = null|bool|int|float|string;

I’m wondering if null should maybe be left out of these, since ?mixed and ?scalar can be used for this?

-- 
Côme Chilliet
FusionDirectory - https://www.fusiondirectory.org

---

## #109749 — Dan Ackroyd — 2020-04-21T11:29:55+00:00

URL: https://externals.io/message/109715#109749 · In reply to: #109720

> I... cannot actually envision what code would actually accept that mess. :-)

 `var_dump()` would be one example. Also caches that can store any
userland value would be another reasonably common use.

The general pattern is when you're handling someone else's 'data' and
have no restrictions on what type that data should be. That's actually
the use-case that prompted me to help draft this RFC.

> now that we have Union types what places are
> there where the currently available type declarations are insufficient?

A type that explicitly allows any type is more forwards compatible
than writing out a union type.

Compare this:

var_dump( mixed $expression [, mixed $... ] ) : void

to:

var_dump( null|bool|int|float|string|array|object|resource $expression
[, null|bool|int|float|string|array|object|resource $... ] ) : void

If/when we add an 'enum' type that is not a child type of any of the
current types, the signature of that function doesn't need to change
if it uses mixed, rather than the written out union type.

Also, the 'fitting on a line' problem.

cheers
Dan
Ack

---

## #109751 — Guilliam Xavier — 2020-04-21T12:26:24+00:00

URL: https://externals.io/message/109715#109751 · In reply to: #109745

On Tue, Apr 21, 2020 at 11:10 AM Côme Chilliet
<come.chilliet@fusiondirectory.org> wrote:
>
> Le lundi 20 avril 2020, 09:15:24 CEST Sara Golemon a écrit :
> > […]
>
> I’m wondering if null should maybe be left out of these, since ?mixed and ?scalar can be used for this?

For mixed, this has been discussed several times, it's defined as the
union of *all* value types (void aside), *including* null, according
to its usage in phpDoc (and in Hack, which also has a nonnull type
such that mixed is equivalent to ?nonnull, cf
https://docs.hhvm.com/hack/types/nullable-types).

For scalar, I already replied
(https://externals.io/message/109715#109723) that it should not
include null indeed (according to
https://www.php.net/manual/en/language.types.intro.php and
https://www.php.net/manual/en/function.is-scalar.php).

--
Guilliam Xavier

---

## #109817 — Gabriel Caruso — 2020-04-23T16:17:02+00:00

URL: https://externals.io/message/109715#109817 · In reply to: #109715

> Hi Internals,
>
> Here is an RFC for adding a 'mixed' type to the language:
> https://wiki.php.net/rfc/mixed_type_v2
> The RFC builds on an earlier draft by Michael Moravec.
>
> Part of the motivation for pursuing this RFC was my recent experience
> in upgrading an application to PHP 7.4.
>
> Being able to add types to the application made the code much easier
> to reason about, but there were places where I couldn't add type info
> due to the lack of mixed. That made it hard to see if that piece of
> code had been upgraded (but type info couldn't be added), or if that
> code still needed to be upgraded.
>
> cheers
> Dan
> Ack
>
> --
> 
> 
>
>
Hello Danack

Has the type `any` been considered? I've discussed that in the
implementation Pull Request with Máté.

Would it be too much to create a sub-voting for the type used here?

Best regards,

---

## #109818 — Ilija Tovilo — 2020-04-23T16:21:07+00:00

URL: https://externals.io/message/109715#109818 · In reply to: #109715



---

## #109819 — Ilija Tovilo — 2020-04-23T16:27:22+00:00

URL: https://externals.io/message/109715#109819 · In reply to: #109818

Sorry, I don't know what's wrong with my mail client, it just sent the
email with no content. Here's it again.

Hi Dan

> Here is an RFC for adding a 'mixed' type to the language:
> https://wiki.php.net/rfc/mixed_type_v2
> The RFC builds on an earlier draft by Michael Moravec.

I don't have a whole lot to add other than I appreciate this RFC. We
currently can't differentiate between "I didn't bother documenting
this" and "yes, I actually accept all types".

One note: I was wondering if the following code throws an error (it does):

function foo(): mixed {}
foo();
//> Uncaught TypeError: Return value of foo() must be of the type
mixed, none returned

I found the answer in the tests. Might be worth mentioning in the RFC.

Regards,
Ilija

---

## #109823 — Bob Weinand — 2020-04-24T12:11:57+00:00

URL: https://externals.io/message/109715#109823 · In reply to: #109715

Hey Dan,

this, I consider one of the few concepts which should never make it into PHP.

In particular, the main problem with mixed is that it is a very easy "I have no idea how to type that properly, so just let's use mixed".

And really, in general, except for some debug functions, mixed is just an excuse to not use proper generics. I cannot think of non-debug scenarios where we are juggling truly unknown types where the type is unidirectional.
(Uni-directional means: there is no corresponding getting/forwarding function for an input.)

Mixed is in almost every case just patching the holes in PHPs type system. And as such, I'd rather promote RFCs trying to properly fix the gaps in the type system than supporting mixed.

Mixed is a hack, do not use mixed.

Bob

P.s.: If you want to migrate your types and have an overview of what's already migrated, maybe just add a comment // type-migrated before or similar? But that's seriously not the business of the language.

> Am 20.04.2020 um 13:17 schrieb Dan Ackroyd <danack@basereality.com>:
> 
> Hi Internals,
> 
> Here is an RFC for adding a 'mixed' type to the language:
> https://wiki.php.net/rfc/mixed_type_v2
> The RFC builds on an earlier draft by Michael Moravec.
> 
> Part of the motivation for pursuing this RFC was my recent experience
> in upgrading an application to PHP 7.4.
> 
> Being able to add types to the application made the code much easier
> to reason about, but there were places where I couldn't add type info
> due to the lack of mixed. That made it hard to see if that piece of
> code had been upgraded (but type info couldn't be added), or if that
> code still needed to be upgraded.
> 
> cheers
> Dan
> Ack

---

## #109824 — Lynn — 2020-04-24T12:18:07+00:00

URL: https://externals.io/message/109715#109824 · In reply to: #109823

> Mixed is in almost every case just patching the holes in PHPs type system.
> And as such, I'd rather promote RFCs trying to properly fix the gaps in the
> type system than supporting mixed.
>
> Mixed is a hack, do not use mixed.
>

Heya Bob,

I maintain lots of legacy code and I cannot add return types despite being
truly mixed. Mixed is not a hack, it's a scenario that frequently happens.
There are lots of use-cases where we explicitly want to mark it as such as
it truly accepts anything. Would I use it in new code? Pretty much never,
as `string|int` would already cover the majority of cases, especially where
ints are represented by strings.

I would love to see mixed being added as it lets me remove a lot of
docblocks that are only added so I can indicate mixed. I disagree that it's
patching holes, it's filling a gap that's currently missing something.

Regards,
Lynn

---

## #109825 — Rowan Tommins — 2020-04-24T13:29:31+00:00

URL: https://externals.io/message/109715#109825 · In reply to: #109824

> I would love to see mixed being added as it lets me remove a lot of
> docblocks that are only added so I can indicate mixed. I disagree that it's
> patching holes, it's filling a gap that's currently missing something.
>


I realise it's partly a matter of style, but if I were benevolent dictator
of a legacy code base, I'd prefer to keep the docblocks, and mandate that
all uses of "mixed" should include a comment describing what the parameter
/ return type represents / is related to. Similarly, I wouldn't delete a
docblock for a return type of "array", because I'd want more specific
details (either using "type[]" syntax, "list<type>" syntax, or a comment
describing the "shape" of the array).

I'm not as strongly against adding "mixed" to the language as Bob, but I'm
not convinced I'd ever bother using it.

Regards,
-- 
Rowan Tommins
[IMSoP]

---

## #109826 — Matthew Brown — 2020-04-24T13:43:15+00:00

URL: https://externals.io/message/109715#109826 · In reply to: #109825

>
> I'm not as strongly against adding "mixed" to the language as Bob, but I'm
> not convinced I'd ever bother using it.
>

I'm opposed to the use mixed in codebases – I try as best I can to remove
it from any code I come across, and a static analysis tool I've built flags
the use of mixed types whenever a more specific type is preferable. I even
have an opt-in website devoted to tracking the percentage of non-mixed
types in open-source projects, e.g.
https://shepherd.dev/github/sebastianbergmann/phpunit

Nevertheless, I think mixed should be available as a type hint for the
extreme cases mentioned in this thread (e.g. serialisation). A type system
without an explicit mixed type feels incomplete to me.

---

## #109827 — Marco Pivetta — 2020-04-24T13:58:50+00:00

URL: https://externals.io/message/109715#109827 · In reply to: #109715

Hey Dan,




> Hi Internals,
>
> Here is an RFC for adding a 'mixed' type to the language:
> https://wiki.php.net/rfc/mixed_type_v2
> The RFC builds on an earlier draft by Michael Moravec.
>
> Part of the motivation for pursuing this RFC was my recent experience
> in upgrading an application to PHP 7.4.
>
> Being able to add types to the application made the code much easier
> to reason about, but there were places where I couldn't add type info
> due to the lack of mixed. That made it hard to see if that piece of
> code had been upgraded (but type info couldn't be added), or if that
> code still needed to be upgraded.
>

How much would you like/be adverse to a rector rule that changes `mixed`
into a long union type (without `resource` in it)?

Considering above posts, `mixed` is effectively
`null|bool|string|int|float|array|object`, and that the language has the
ability to use this type in 8.0, I think making `mixed` very painful to
write may be a big advantage.

`resource` is still a massive PITA: not sure if it can be removed within
the decade.

Heck, we may even deprecate signature-less parameters and return types in a
very far future 💪

---

## #109828 — Bob Weinand — 2020-04-24T14:33:24+00:00

URL: https://externals.io/message/109715#109828 · In reply to: #109823

Actually,

I forgot that for proper generics implementations, collections etc. will obviously need to specify "allowing any type". As such the introduction of mixed is pretty much necessary.

As in class Foo<A, B> { … } $foo = new Foo<mixed, int>;

As such, I'm actually in favor of introducing it.

Bob

> Am 24.04.2020 um 14:11 schrieb Bob Weinand <bobwei9@hotmail.com>:
> 
> Hey Dan,
> 
> this, I consider one of the few concepts which should never make it into PHP.
> 
> In particular, the main problem with mixed is that it is a very easy "I have no idea how to type that properly, so just let's use mixed".
> 
> And really, in general, except for some debug functions, mixed is just an excuse to not use proper generics. I cannot think of non-debug scenarios where we are juggling truly unknown types where the type is unidirectional.
> (Uni-directional means: there is no corresponding getting/forwarding function for an input.)
> 
> Mixed is in almost every case just patching the holes in PHPs type system. And as such, I'd rather promote RFCs trying to properly fix the gaps in the type system than supporting mixed.
> 
> Mixed is a hack, do not use mixed.
> 
> Bob
> 
> P.s.: If you want to migrate your types and have an overview of what's already migrated, maybe just add a comment // type-migrated before or similar? But that's seriously not the business of the language.
> 
> > […]

---

## #109829 — Markus Fischer — 2020-04-24T14:48:59+00:00

URL: https://externals.io/message/109715#109829 · In reply to: #109828

> Actually,

Really had to laugh, reading your previous and then this mail :-)


> 
> I forgot that for proper generics implementations, collections etc. will obviously need to specify "allowing any type". As such the introduction of mixed is pretty much necessary.
> 
> As in class Foo<A, B> { … } $foo = new Foo<mixed, int>;
> 
> As such, I'm actually in favor of introducing it.

Re-quote:
 > "allowing any type"

That would make the case for the `any` type instead of mixed; IMHO much 
more clear.

- Markus

---

Next page: https://externals.io/message/109715.md?page=2
