Re: Allow Iterator to be used with current, next, reset, key functions
| From: | Yasuo Ohgaki | Date: | Sun, 30 Oct 2016 23:25:56 +0000 |
| Subject: | Re: Allow Iterator to be used with current, next, reset, key functions | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-96668@lists.php.net to get a copy of this message | ||
Hi, Fleshgrinder
On Mon, Oct 31, 2016 at 8:10 AM, Fleshgrinder <php@fleshgrinder.com> wrote:
> On 10/30/2016 11:59 PM, Yasuo Ohgaki wrote:
>> There should be delete option. It's more useful if there is this option.
>>
>> mixed array_first(array $arr [,bool $get_key = FALSE [, bool
>> $remove_entry=FALSE]]);
>> mixed array_last(array $arr [,bool $get_key = FALSE [, bool
>> $remove_entry=FALSE]]);
>>
>> Problem with this is error condition. When array is empty, what should
>> be returned from these with default option? Returning NULL for empty
>> array would be enough. IMO.
>>
>
> Flags are usually a bad design decision, it's better to make it fully
> explicit and provide dedicated APIs for everything. This is much easier
> for the reader and reading is more important than anything else.
It depends. Defining specific functions for a task could be bad design
also. We do design basic functions with flags usually, too.
That said, dedicated functions for getting first/last key/value sounds
good to me.
>
> ```
> /** Get the first element in the array. */
> function array_first(array $array);
>
> /** Get the key of the first element in the array. */
> function array_first_key(array $array);
>
> /** Get the last element in the array. */
> function array_last(array $array);
>
> /** Get the key of the last element in the array. */
> function array_last_key(array $array);
>
> /** Get and remove the first element from the array. */
> function array_take_first(array $array);
>
> /** Get the key of the first element in the array and remove it. */
> function array_take_first_key(array $array);
>
> /** Get and remove the last element from the array. */
> function array_take_last(array $array);
>
> /** Get the key of the last element in the array and remove it. */
> function array_take_last_key(array $array);
> ```
>
> Returning
null is imho the most sensible thing we can do if the
> array
> is empty.
A agree. That's the reason why I state return value is problem. However,
if (empty($arr)) {
// Do something
} else {
$first = array_first($arr);
}
shouldn't be difficult.
We should raise error for nonsense usage. Programmers have to check
empty()/isset() all the time anyway.
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net