Hi Mike, Thanks for your support, and yes, you're correct, I did mean to structure the type prior to the [].
I'm unsure of exactly how this might work so defer to an Internals export, but having previously read @Nikita Popov <nikita....@gmail.com>'s great post on PHP's arrays, I did wonder if by knowing the data type within an array and that it'd conform to a strict structure, could the array itself be stored in an alternative way in C? Perhaps a more memory efficient way, or one that's faster to iterate over rather than just a hash table? Link to this article for reference: https://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html Cheers, Aran On Fri, 17 Jan 2020 at 19:17, Mike Schinkel <m...@newclarity.net> wrote: > > > On Jan 17, 2020, at 2:50 AM, Aran Reeks <cdtre...@gmail.com> wrote: > > 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. > > I for one would be a big +1 for this, with caveats. > > > IDEs like PHPStorm handle this structure already hence sticking to that > as > > a starting point... > > > > @returns []int > > As previously noted, I assume you meant int[]? > > ---- > > Having the ability to type array elements would cover ~90% of the cases > where I cannot properly type parameters or return values in PHP 7.4. > > The caveat is that it would seem that to check dynamically would be an > expensive proposition such as when an array that was not typed is passed to > or returned from a function or assigned to a variable of a declared type, > e.g. > > function foo( $myarray ): Foo[] { > return $myarray; // This would need to be dynamically checked, I > think? > } > > Of course we could limit this type of typing to only arrays that are > already know to be typed, meaning this would always fail: > > function bar( Foo[] $myarray ) { > // Do whatever > } > function baz( $myarray ) { > bar( $myarray ); // Fails here because $myarray not known to be > Foo[] even when it is > } > baz( [ new Foo() ] ); > > Alternately we could have a global option that would do type checking or > not for these type hints, so they could be dynamically checked for all code > prior to production code, where checking could be turned off. > > Another option could be if the number of array elements is small (<100?) > it could check, but otherwise not check, but this feels all kind of > different types of wrong. > > ---- > > My vote, if I had one, would be to add type new typing for array elements, > but also add a type checking global option that can be in one of 3 states: > > 1. Static checks only, > 2. Dynamic checks only, or > 3. No checking of array elements. > > #jmtcw > > -Mike > P.S. Or maybe there is an inexpensive way to keep track of the types of > the entire array on element assignment? > P.P.S. Can someone please explain and give an example of how generics > would make this need moot? I do not get why that would be the case... > >