On Thu, Jan 25, 2018 at 2:10 PM, Rowan Collins <rowan.coll...@gmail.com> wrote:
> On 25/01/2018 14:52, Derick Rethans wrote: > >> IMO, it makes a lot more sense to check integrity when creating the >> "array" structure. Instead, I would suggest to add a native Collection >> type, that takes a "type" as argument. They aren't quite full generics, >> but it does 1. fix something; 2. isn't really complicated. >> >> What I am suggesting is to add a new syntax "Collection<$type>", >> mimicking a class, but having a type as "argument": >> >> $a = new Collection<integer>; >> > > So would I be right in thinking this would mean adding basic support for > generics to the engine, but not letting new ones be defined in userland? > So, a bit like how internal or extension classes have the ability to do > more overloading of casts and operators than userland classes? > > If so, I like the concept, but wonder whether the parts of the > implementation it would need are the easy parts of the hard ones. I guess > not being able to inherit from the generic would get rid of a bunch of > complexity, but don't really know where else the challenge lies. > He's getting the syntax from Java. Java collections allow you to specify the type they are constrained to in <> after the class name. Type Script does something similar. The problem is that Java already has the policing mechanics present in the runtime to police insertions into the collection and guarantee they are of the correct type. PHP has no such mechanism in place and in previous discussion threads the primary maintainers have underscored that adding such is highly difficult. It is also completely tangential to and a distraction from what I propose and would like to focus on here. Regardless of whether PHP ever gets a core class or construct for type uniform collections, the ability to verify that variables are a "collection of something" will remain. That is the issue I want to discuss and address here - verifying that existing variables are a given collection type, not debating pie-in-the-sky, may never be implementable classes that self insure their own integrity. So, given `$a collectionof string` the operator returns whether or not $a is, at that time, a collection of strings (be it an array or other iterable). It doesn't insure $a will stay that way - it's just a check of the variables status at a given moment, which is the best that can be hoped for in PHP.