On Tue, Feb 23, 2021 at 9:31 AM Nikita Popov <nikita....@gmail.com> wrote:

> On Fri, Feb 12, 2021 at 8:25 PM David Rodrigues <david.pro...@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;
> >
> > So $companies will be null except if the user has companies.
> >
> > My suggestion is create some kind of inline conditional to works like
> that:
> >
> > $companies = $user->companies->count() => new
> Collection($user->companies);
> >
> > If the conditional ($user->companies->count()) is true, then will return
> > the value (after =>), else will be null.
> >
> > In my current work project, I have more than 100+ occurrences like that.
> >
> > So some more examples:
> >
> > $userCategory ? $userCategory->getTitle() : null
> > -> It could be optimized to the new nullsafe operator
> > $userCategory?->getTitle()
> >
> > return $languageFirst instanceof LanguageExperience
> > ? $languageFirst->title : null;
> > -> This not, with my suggestion we have:
> > return $languageFirst instanceof LanguageExperience =>
> > $languageFirst->title;
> >
> > The => is just a suggestion, other options using existing keywords is:
> > return expr() then output();
> > return expr(): output();
> >
> > I do not know if other languages support something like that.
> >
> > Thanks!
> >
>
> There's a limited budget for this kind of syntax sugar, and we already have
> ?:, ??, ??=, ?->. I don't think there's space for yet another shorthand
> conditional syntax.
>
>
I don't really have any strong feelings on this either way, but ?! seems
like a logical choice if it was to be implemented.


> Note that => cannot be used for this purpose, as it is already used for
> array literals.
>
> Regards,
> Nikita
>


-- 
Chase Peeler
chasepee...@gmail.com

Reply via email to