On 25/01/2018 20:19, Michael Morris wrote:
He's getting the syntax from Java.

Actually, from C++, and possibly from somewhere before that; at this point, it's pretty widely adopted.


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.

That's only partially true. As Bishop Bettini pointed out, you can *almost* get there with features already in the language, because you can implement the magic ArrayAccess interface and intercept all assignments using the $foo[]=$val and $foo[$bar]=$val syntaxes.

The main problem, as usual, is references: you can't intercept "$ref =& $foo[$bar]; $ref=42;" - if you try, you get a somewhat cryptic notice that "Indirect modification of overloaded element of foo has no effect". Apart from that, this is a *lot* simpler as a problem than general type tracking, because we only care about those two specific operations, not everywhere that performs an assignment of any sort.

In that sense, a full implementation of generics is actually simpler: the only actual type checks would be on parameters and return types, where they're already supported, e.g. class Stack<T> { public function push(T $item) { ... } public function pop(): T { ... } }

Like I say, it might be perfectly reasonable to have both a collectionof operator and generics in the language, but I don't think it's unreasonable to think about where they overlap.

Regards,

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to