On Fri, Jan 17, 2020 at 8:51 AM Aran Reeks <[email protected]> wrote:

> Hi Internals,
>
> I'd like to kick off a conversation to capture everyone else's thoughts on
> tweaking / improving typed properties for arrays (for a PHP 8.x release).
>
> With all the work done lately to greatly improve the type support in PHP
> (which is amazing by the way), I'm finding for the most part, I'm no longer
> needing to Docblock as much of my code which is lovely.
>
> That said, there's a common use case that keeps me going back to them which
> I think would be a good thing for PHP to try and solve as a language
> feature - better typing of arrays to type their properties.
>
> IDEs like PHPStorm handle this structure already hence sticking to that as
> a starting point...
>
> @returns []int
>
> This would designate the return of an array where all its keys are that of
> the int type, but it works for any type.
>
> With that in mind, it might also make sense to allow a shorthand array
> alias for array types anyway - array -> [].
>
> To use actual PHP examples, this would mean the following would be
> supported:
>
> // Typed array properties ...values would follow any existing PHO type
> function returnsIntArray(): []int;
> function returnsClassArray(): []Class;
>
> // The same outcome
> function returnsArray(): array;
> function returnsArray(): [];
>
> I welcome all your thoughts on this proposal.
>
> Many thanks,
> Aran
>

Hi Aran,

Did you read through the previous discussions on this topic?
https://externals.io/message/100946 in particular comes to mind.

The primary concern about the previous typed array proposal was the O(n)
cost of type checks, which required iterating over the whole array and
checking the type of individual elements. Any new proposal in this area
*must* address this concern.

As far as I know, the only viable way to do that is to make the array
intrinsically typed, which means that types are validated when elements are
inserted into the array, not when it is passed across a function boundary.
In other words, array generics.

Regards,
Nikita

Reply via email to