Mixed type-hint

php.internals

Rasmus Schultz

7 years ago
Regarding the mixed type-hint RFC: https://wiki.php.net/rfc/mixed-typehint
> When no native type is present, it is unclear what type is expected
Regarding variance - in the examples shown in the RFC, it is still unclear whether your intention was variance, or if you just omitted the type-hint by accident. The introduction of the mixed type-hint, as I was trying to explain earlier, could have addressed that problem - by allowing mixed variance only with an explicit type-hint and disallowing implicit variance. As I pointed out when the variance RFCs were accepted, it's too late to remove this allowance now, as that would be a breaking change. However, could we at least make it an E_STRICT? So that, given the following: class A { public function foo(int $value) {} } The following would emit an E_STRICT: class B extends A { public function foo($value) {} // E_STRICT: implied variance } And the following would not: class B extends A { public function foo(mixed $value) {} // explicit variance } An E_STRICT is not a breaking change, right? At least then we can map E_STRICT to an ErrorException during development. Without this, the mixed type-hint is basically meaningless noise, is it not? About as effective is a doc-block?

Robert Korulczyk

7 years ago
> Without this, the mixed type-hint is basically meaningless noise, is it > not? About as effective is a doc-block?
This mixed type seems to be meaningless by design since its main goal is to work the same as if it was no type-hint at all... Another thing is that including null as part of mixed is not very pragmatic - mixed could be used to disallow null, so it will actually work for type check. Regards, Robert Korulczyk

Marco Pivetta

7 years ago
Mixed includes null by definition. On Fri, 8 Feb 2019, 12:21 Robert Korulczyk <robert@korulczyk.pl wrote:

Robert Korulczyk

7 years ago
What definition? Regards, Robert Korulczyk W dniu 08.02.2019 o 12:37, Marco Pivetta pisze:

Marco Pivetta

7 years ago
`mixed` is the "top" type, which means that it contains anything at all. See https://en.wikipedia.org/wiki/Top_type On Fri, 8 Feb 2019, 12:45 Robert Korulczyk <robert@korulczyk.pl wrote:

Robert Korulczyk

7 years ago
Sounds like an arbitrary interpretation. Treating `?mixed" as "top type" (including null) would be more practical and consistent with other type-hints. Regards, Robert Korulczyk W dniu 08.02.2019 o 12:47, Marco Pivetta pisze:

Marco Pivetta

7 years ago
On Fri, 8 Feb 2019, 13:13 Robert Korulczyk <robert@korulczyk.pl wrote:
> Sounds like an arbitrary interpretation. Treating `?mixed" as "top type" > (including null) would be more practical and consistent with other > type-hints. >
The PHP ecosystem (libraries) already uses `mixed` as a type including `null`. You can survey for usages of `mixed|null`, which is currently normalised to `mixed` by existing static analysis tooling (psalm, phpstan). Even then, by adding a non-nullable almost-top-type doesn't seem helpful: "here's something random, just make sure it isn't null". That's something more interesting for an eventual union type RFC (revival?). Could you clarify on a use-case for changing the semantics of `mixed`?

Robert Korulczyk

7 years ago
> Could you clarify on a use-case for changing the semantics of `mixed`?
For example storage which does not allow to store null. Like simple cache which treats null as "miss", so it is not able to cache null as value. Obviously it is not a deal breaker and use case is quite rare anyway, but this is just example when mixed type-hint may work as actual type-hint, not just replacement for PHPDoc or equivalent of... nothing. Regards, Robert Korulczyk

Girgias

7 years ago
On Fri, 8 Feb 2019 at 13:13, Robert Korulczyk <robert@korulczyk.pl> wrote:
> Sounds like an arbitrary interpretation. Treating `?mixed" as "top type" > (including null) would be more practical and consistent with other > type-hints. > > Regards, > Robert Korulczyk >
The whole PHP Documentation is based around the fact that `mixed` includes `null`, would it make more "sense" that `null` is not included, probably, also even if I do like the proposal it seems this is just to add a type-hint so you can *mostly* type hint all function. But in this regards I would prefer that the scalar RFC type hint goes back into discussion as it has potentially more use case IMHO. Best regards George P. Banyard

Dan Ackroyd

7 years ago
On Fri, 8 Feb 2019 at 07:20, Rasmus Schultz <rasmus@mindplay.dk> wrote:
> > Regarding the mixed type-hint RFC: > > https://wiki.php.net/rfc/mixed-typehint
The status of the RFC is in "In Draft" and you don't appear to be the author of the RFC. It is not appropriate for someone other than the author to start the discussion of an RFC. Please can everyone wait until the author of the the RFC has finished writing it, before saying that it's wrong? cheers Dan