Re: Introducing "Array Of" RFC
On 20/01/14 22:05, Stas Malyshev wrote:
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.
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/
Thread (73 messages)