> The Collection is Iterable, Traversable, Countable like an array to be > compatible with foreach and other Traversable-compatibles functions.
Right, so it's an array. Except, it's an object-type, rather than a value-type like arrays. (I'm guessing - I don't think you specified?) And it's type-checked. So what you're really describing is generic arrays: https://wiki.php.net/rfc/generic-arrays I would strongly prefer we don't venture into generics for isolated cases like collections. I would also strongly prefer a generic collection-type that is a value-type, rather than an object. (which may or may not be what you're proposing?) I would also strongly prefer an approach that builds on the foundation of arrays, rather than tries to replace them - introducing yet another collection-type creates even more disparity in the language, e.g. yet another collection-like thing that's passed by reference and begets side-effects, surprising behavior, and bugs. Introducing generic features like this one in small increments, in my opinion, is harmful in the long term - it would be much better to introduce generics consistently, across the board, as a language feature, rather than as a use-case-specific feature. This is my general opinion about languages. Go, for example, has 3 or 4 use-case-specific generic features, but doesn't have generics as a language feature, and it's horrible - it puts you in the mode of programming with generics for those use-cases, and then having to come up with complex architectural work-arounds when you venture outside the confines of the 3-4 use-cases the design team thought were important. This sort of thing begets inconsistency, and PHP has too many as it is. Just my two cents. On Sat, Jan 28, 2017 at 4:37 PM, georges <geol...@gmail.com> wrote: > Hello > > Php Internals, > > I was wondering today why did we not have (yet ?) > > a Collection type in Php ? > Typically what would be that kind of variable type ? > > This type would be a great intermediate between an array and an Object. > Today, in my opinion we are seeing to much wrong uses of php Object. > To quote one of the most used: The Doctrine Collection object. > These kind of object are IMO very memory-greedy ones. > > The implementation idea would be to have a new variable type: > The Collection. > The Collection is Iterable, Traversable, Countable like an array to be > compatible with foreach and other Traversable-compatibles functions. > The Collection become a type on it's > > own, so there's a lot of changes to be considered: > - The serialization > - The vardumping/exporting > - The Exception traces > - The backtrace > > To be syntaxely compliant to array and object it would be case insensitive > even if i would like to prefer it Camelcased: Collection. This is up to > you. > The target is Php 8, but the reserved word could be introduced in php 7.3. > > Here's are very basics implementation concepts: > > /** > * Standard var declaration > */ > $collection = Collection(int); > > /** > * Alternative var declaration > */ > $collection = Collection(int, [1, 2, 3]); > > // Throws nothing > $collection[]= 10; > $collection['abc']= 10; > > // Throws CollectionTypeHintError > $collection = Collection(int, [1, '2', 3]); > $collection['abc']= '10'; > $collection['abc']= new StdClass; > > // Transtypes the collection to an standard array > $collectionAry = (array) $collection; > > // Throws CollectionTypeReHintingError > $collection = (array) $collection; > $collection = 10; > > // Throws nothing > unset($collection); > $collection = 10; > > > Now the +/-; > > The +: > + We have an optimized Collection type instead of those ugly Object-like > collections that are ressources-greedy. > + Aside of the optimization, we have a well understandable type hint > reserved to the Collections. This is clearly a better > semantically-appropriated way to work with Collection data. > > > The -: > - Collection become a reserved word. Therefore, this could introduce a > major BC. > > > Cheers, > Georges.L >