Re: Typed array properties V2
| From: | Brent Roose | Date: | Fri, 17 Jan 2020 08:16:39 +0000 |
| Subject: | Re: Typed array properties V2 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108177@lists.php.net to get a copy of this message | ||
Hello all
It's a much-requested feature for years and years. My first thought was "we need generics,
not this" but than I took 5 minutes to actually think about it. While the same, and much more,
can be achieved with generics, it's a difficult feature to implement. There have been several
RFCs for generics in the past, which failed. I know Levi Morisson was, at one point, looking at
adding support for generics only in traits, because it's difficult to add them in other places.
While I still think generics would be a great feature, I now also believe it's worth looking at
an "array of" type as something standalone. I've got no clue about the technical
implications, but maybe suprting "array of" syntax is a lot more easy than full blown
generics? Looking at my day to day work with PHP, I'dsay "array of" types would solve
~80% of my problems with PHP's current type system, and I figure there are lots of developers
in a similar situation. If I remember correct from my college days, Java also supports both styles:
Int[] and ArrayList<Int>.
All that to say that maybe it's worth the effort looking at "array of" types as
something different than generics?
Kind regards
Brent
> On 17 Jan 2020, at 08:50, Aran Reeks <cdtreeks@gmail.com> 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