Re: Introducing "Array Of" RFC
| From: | Stas Malyshev | Date: | Mon, 20 Jan 2014 22:05:56 +0000 |
| Subject: | Re: Introducing "Array Of" RFC | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-71364@lists.php.net to get a copy of this message | ||
Hi!
> 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.:
>
> $fruits = Array<Fruit>; - declare that $fruits is an array that can only
> contain instances of class Fruit
> function eat(Array<Fruit> $apples) - function eat() will only accept
> arrays that are declared in the same way as $fruits above
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.
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227