Re: Introducing "Array Of" RFC
From the survey I posted (
http://grokbase.com/p/php/php-internals/141rva4cf7/php-dev-vote-array-of-v-generics
) the most popular option seemed to be "C) I want both".
While I am not just blindly suggesting we copy Java, they do have both
features with different syntax, so the idea for this RFC is going to
be: keep on trucking with the array of syntax and we put it to a vote.
If people do not like the RFC as it is then we can revisit the syntax,
but as people are interested in both features it just makes sense to
assume this syntax is up for the job.
The only open question remaining is:
Should null be an acceptable value inside the array, or can it only
contain items of the type specified?
The following equivalent code will explain the difference:
A)
foreach ($foos as $foo) {
if (! $foo instanceof Face) {
throw new Exception ('AAAGGGGGHHH!');
}
}
or
B)
foreach ($foos as $foo) {
if (! is_null($foo) and ! $foo instanceof Face) {
throw new Exception ('AAAGGGGGHHH!');
}
}
Logic suggests to me that A makes more sense, as if you ask
specifically for an array of "Face" and want to act on it like every
single item is an instance of "Face", then letting null's slip into
there just adds more boilerplate and introduces a second - and totally
uneccessary - array traversal to find them or remove them.
So, should we allow nulls in an arrayof-style type hint?
Thread (73 messages)