Another example is when a scalar input needs to be either left null or converted to a Value Object:
$color = $data['color'] ? new Color($data['color']) : null; This would become; $color = $data['color'] ? new Color($data['color']); //the ": null" part is implicit It's actually kinda the inverse of the "?:" operator. As Guilliam said, Twig already implements such behaviour, it's quite handy when dealing with nullable variables, On Tue, Feb 23, 2021 at 4:26 PM Chase Peeler <chasepee...@gmail.com> wrote: > 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 >