Re: [RFC] anti-coalescing-operator
| From: | Claude Pache | Date: | Fri, 25 Oct 2019 13:18:19 +0000 |
| Subject: | Re: [RFC] anti-coalescing-operator | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107698@lists.php.net to get a copy of this message | ||
> Le 25 oct. 2019 à 04:36, Ken Stanley <dohpaz@gmail.com> a écrit :
>
> So far it seems like the biggest concern(s) is that we would most likely
> repeat the use of variables (nothing new), and that there are other ways to
> do the same thing (again, nothing new). And of course the most valid
> argument of all: adding more operators to the soup mix. Personally, I don’t
> see these as show stoppers, at least given that the idea is not too wild
> and out there.
Hi,
If you write
A !?? B
instead of
isset(A) ? B : null
you gain a *fixed* amount of 12 characters. The gain is proportionally low when A and B are not
trivial expressions. (The difference is fixed, because the proposed operator does not allow to
reduce possible repetition of expressions.)
I don’t think that you gain readability or clarity. In particular, I am particularly confused with
your last example:
> /**
> * @ParamConverter(name=“application”, ...)
> */
> public function myAction(Request $request, Application $application)
> {
> $user = $application->getUser() !?? $this->getUser();
>
> // ... do something with user, without worrying about it being null.
> }
because if $application->getUser() is null, then $user will be null, and you do have to worry
about it being null, which makes me wonder whether there is some bug here. If it is really what you
meant, spelling *null* explicitly will make it clearer, despite of being 12 characters longer. And
if it is not what you meant, the bug will appear more readily:
/**
* @ParamConverter(name=“application”, ...)
*/
public function myAction(Request $request, Application $application)
{
$user = isset($application->getUser()) ? $this->getUser() : null;
// ... do something with user, without worrying about it being null. (sic)
}
―Claude