Re: [RFC] [Discussion] array_first() and array_last()

From: Date: Tue, 08 Apr 2025 18:46:16 +0000
Subject: Re: [RFC] [Discussion] array_first() and array_last()
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


> Le 5 avr. 2025 à 17:51, Niels Dossche <[email protected]> a écrit :
> 
> Hi internals
> 
> I'm opening the discussion for the RFC "array_first() and array_last()".
> https://wiki.php.net/rfc/array_first_last
> 
> Kind regards
> Niels


Hi,

I think that this argument is not convincing, and even counterproductive:

* NULL is a rare legitimate value, so the potential for clashing is low

First, it says that it is a “rare” legitimate value, which one can disagree with. (I do
disagree.)

Second, the way it is formulated, it implies that, when null is used in array, there
will be “clashing”, which is not necessarily the case. (I consider that there is almost never a
clash, because it is rarely useful to make the difference between an explicit null and
a missing value, and, when it is useful, you have almost surely already checked for an empty array
upfront.)

I suggest to replace that argument with the two following ones, that doesn’t treat arrays with
NULL as second class citizens:

* Semantically, NULL represents a missing value. Returning NULL from an empty array is not
semantically incorrect, but it means that the function doesn’t differentiate between a implicit
missing value (empty array) and an explicit missing value (array with NULL as its first/last
element).

* In the relatively rare cases you do want to make the difference between an empty array and an
array that starts/ends with NULL, you can (and should) just check for empty array upfront.

-------

One more thing. On https://www.php.net/, I read: “Fast,
flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the
world.” (emphasis added). If we were to design some new perfect language, we might consider making
array_first() (or equivalent) choke on empty arrays. But given the current state of the
affairs, the pragmatic thing to do is to pave the cowpath.

—Claude


Thread (22 messages)

« previous php.internals (#127076) next »