Re: [RFC] Change behaviour of array sort functions to return a copy of the sorted array

From: Date: Mon, 21 Oct 2024 16:28:56 +0000
Subject: Re: [RFC] Change behaviour of array sort functions to return a copy of the sorted array
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Mon, 21 October 2024 г. at 18:13, Gina P. Banyard <[email protected]> wrote:
>
> On Monday, 21 October 2024 at 14:29, Ilija Tovilo <[email protected]> wrote:
>
> > Hi Gina
> >
> > On Mon, Oct 21, 2024 at 3:21 PM Gina P. Banyard [email protected] wrote:
> >
> > > On Sunday, 20 October 2024 at 18:42, Gina P. Banyard [email protected] wrote:
> > >
> > > > https://wiki.php.net/rfc/array-sort-return-array
> > >
> > > For an example, I'm going to pull out my solution to day 1 of the 2022 advents
> > > of code:
> > >
> > > https://github.com/Girgias/advent-of-code/blob/19283e1f5ef503c8a4478e58aaa57ff2fb7164c7/2022/01/puzzle.php#L25
> > >
> > > However, if the sort functions would return a useful value instead of
> > > true I could chain it together completely as follows:
> > >
> > > $top3 = array_slice(
> > > rsort(
> > > array_map(
> > > 'array_sum',
> > > array_map(
> > > fn($v) => explode("\n", $v),
> > > explode(
> > > "\n\n",
> > > $input
> > > )
> > > )
> > > )
> > > ),
> > > 0,
> > > length: 3
> > > );
> >
> >
> > Note that your example would still warn after this RFC, which is
> > likely enough to deter people from writing code like this.
> >
> > https://3v4l.org/mRuoK
>
> There is a very simple fix, which is to add the /** @prefer-ref $array */ doc comment to the
> stub of the relevant functions.
> Something that array_multisort() already does.
> But thanks for reminding me how crap PHP references are :D
>
> I'll amend the RFC soon to include this change as part of the proposal.
>
> Best regards,
>
> Gina P. Banyard

Hi, Gina!

What if instead of this proposal we reimplement all of the array
functions in a different namespace and fix a lot of other problems and
inconsistencies?

Array\map(iterable $iterable, callable $mapper): array
Array\sort(iterable $iterable, int $flags = SORT_REGULAR): array
...

-- 
Valentin


Thread (24 messages)

« previous php.internals (#125826) next »