Re: Introducing "Array Of" RFC
| From: | Andrea Faulds | Date: | Mon, 20 Jan 2014 22:33:21 +0000 |
| Subject: | Re: Introducing "Array Of" RFC | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-71367@lists.php.net to get a copy of this message | ||
On 20/01/14 22:05, Stas Malyshev wrote:
Hi!Myself, I want 2 now with syntax that will eventually work for 1 too in the future. Soft generics. Array could implicitly cast to Array<Fruit> so long as all its members are fruit, in future. But for now, it will only be 2. This adds an incentive to add generics: better performance by not requiring a runtime check when an Array<Fruit> is passed an not an Array of Fruit. -- Andrea Faulds http://ajf.me/Please excuse my lack of knowledge, but if I understand this correctly, this is how generics would work ... using arrays, ignore the syntax and details such as child classes, interfaces, etc.:There's two ways to understand "will only accept": 1. There's a class/type which I call ArrayOfFruit for simplicity. eat() checks if $apples is instance of ArrayOfFruit. Some magic inside ArrayOfFruit ensures ArrayOfFruit always contains only Fruit. 2. eat() goes over $apples which is a regular array, and checks if every element in it is instanceof Fruit. First way is essentially templates, second is not. So far I've seen most advocacy in this RFC to go towards the second way.$fruits = Array<Fruit>; - declare that $fruits is an array that can onlycontain instances of class Fruitfunction eat(Array<Fruit> $apples) - function eat() will only acceptarrays that are declared in the same way as $fruits above