Re: Inline conditional that returns null if falsy

From: Date: Sat, 13 Feb 2021 03:11:26 +0000
Subject: Re: Inline conditional that returns null if falsy
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-113164@lists.php.net to get a copy of this message
Hey David,

On Fri, Feb 12, 2021, 20:24 David Rodrigues <david.proweb@gmail.com> wrote:

> Hello!
>
> It is just a suggestion to be discussed.
>
> A lot of places on my projects I have codes like:
>
> $companies = $user->companies->count()
>     ? new Collection($user->companies)
>     : null;
>

Even upfront, this looks like a quite bad idea: either you have an empty
collection, or you have a collection with elements in it, but null is a
very different concept that isn't homogeneous with the resulting type.

I've updated multiple projects in the past where array|null was used as
return type for a collection-alike endpoint, removing null and replacing
it with an empty iterable instead.

>


Thread (23 messages)

« previous php.internals (#113164) next »