On Tue, Oct 17, 2023 at 11:22 AM Lynn <kja...@gmail.com> wrote:
>
>
>
> On Tue, Oct 17, 2023 at 11:15 AM Robert Landers <landers.rob...@gmail.com> 
> wrote:
>>
>> On Tue, Oct 17, 2023 at 11:10 AM Levi Morrison via internals
>> <internals@lists.php.net> wrote:
>>
>> How is returning null any different than $array[0] on an empty array?
>> https://3v4l.org/>
>
> > > c) Two such functions were proposed and rejected during the
> > > array_key_first/last RFC
> > > (https://wiki.php.net/rfc/array_key_first_last)
> > >
> > > Yes, that was in 2018. At that time, functions like str_contains() or
> > > str_starts_with() wouldn't have even come into existence, just because
> > > there was an obscure way to do it without them. I believe we've moved
> > > on since then. Today we know how useful it is to use simple,
> > > easy-to-understand methods, both for programmers who write and read
> > > the code.
> >
> > It's true that sentiment may have shifted in this time. However, a
> > common argument at that time still stands: `null` is not a good
> > sentintenal for failure because the value inside the array very well
> > could have been null. This is not true for the keys. For me
> > personally, I think I would still vote no. I'm not entirely sure about
> > that, but that's how I would lean right now.
> >
> > As it stands, you'd have to write code along the lines of:
> >
> > ```php
> > $key = \array_key_first($array);
> > if ($key === null) {
> >     // handle the failure
> > } else {
> >     // success
> >     $value = $array[$key];
> > }
> > ```
> >
> > Yes, it would be slightly nicer if we could do:
> >
> > ```php
> > $value = \array_first($array);
> > if ($value === null) {
> >     // handle the failure
> > } else {
> >     // success
> > }
> > ```
> >
> > But I fear in practice people will just omit the error checking.
> >
> > One way around that is to throw an exception. I'm not sure how I feel
> > about that, but I'll think about it.
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: https://www.php.net/unsub.php
>>
>> >K3gRs
>>
>> Currently, it just outputs a warning, but the result is `null`.
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: https://www.php.net/unsub.php
>>
>
> I'm okay with `null` being given back as it's the current behavior of 
> accessing the key as well, which is often checked through `$array[0] ?? 
> null`. Something like an `undefined` type could solve this problem, but 
> personally not a fan of this in Javascript. Maybe `array_has_first` or 
> something could be made, but honestly I would still just check for `null` 
> myself most likely.

Huh, that's a good observation Lynn. If I might suggest an API change
to `array_value_first` (and a similar one for array_value_last), it
would be this: `array_value_first(array $array, string|int|null &$key
= null): mixed` where you also get back the key. If you want to know
if null is an error or an actual value:

$value = array_value_first($array, $key);
if($key === null) // handle error
else // do something with $value

You can also freely ignore the key, if you don't need it or care about
error checking. That would save doing two function calls on the same
array, just to do some error checking.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to