Re: RFC: Marking return values as important (#[\NoDiscard])

From: Date: Thu, 20 Feb 2025 19:53:04 +0000
Subject: Re: RFC: Marking return values as important (#[\NoDiscard])
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-126468@lists.php.net to get a copy of this message
On Thu, Feb 20, 2025, at 16:54, Volker Dusch wrote: > > On Wed, Feb 12, 2025 at 9:57 PM Rob Landers <rob@bottled.codes> wrote: > Hey Rob, > > Sorry for writing the mail again, I just noticed I forgot to include the list on my first reply > to you, also corrected a mistake in the second paragraph. > > > There will eventually be a php 9, where BC changes will be possible. > > I don't assume PHP will change the behavior of widely used core functions to throw > exceptions in PHP 9 as the BC impact will be colossal, hurting adoption. Or am I misunderstanding > you here? > > The point that there might be arguments over usage in internal code is why we went with an > "only if it is a clear problem, especially when the function has important side effects" > policy to avoid this. There are not a too many examples in core, more in userland. I was merely pointing out that if you were to want to make BC breaks, you can. You just won’t get it :soon: > > > I have to stop you here. It is absolutely NOT safe and reasonable to ignore the output of > > array_pop. You popped it for a reason. If you just care about removing the last element, then you > > should use unset. Unset gives the intention. If I review code with array_pop, I’ll spend quite a > > bit of time checking that it was intentionally ignoring the return value (and leave a comment of the > > same, asking to use unset instead). > > There are plenty of codebases where array_pop is used for this reason. Identifying the last > element of a php array. unset($foo[array_key_last($foo)]); is only > possible since 7.3 and not widely used. array_pop is also shorter and faster for the same effect > (when used on unknown array shapes ofc). The examples are plenty > https://github.com/search?q=repo%3AWordPress%2FWordPress%20array_pop&type=code > > Regards, > Volker This feels like a rationalization, and not a reason. It has a return value, and there are more expressive ways to remove a value from an array that makes the intention clearer. If you aren’t using the return value, you should make it clear that is the intention. That’s why I said it should be included in this, for exactly the rationale you gave. It IS faster, but it clearly has a side effect that should be used or intentionally discarded. — Rob

« previous php.internals (#126468) next »