Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113226 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 83309 invoked from network); 23 Feb 2021 15:38:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Feb 2021 15:38:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9D2761804B3 for ; Tue, 23 Feb 2021 07:26:46 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f42.google.com (mail-ua1-f42.google.com [209.85.222.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 23 Feb 2021 07:26:46 -0800 (PST) Received: by mail-ua1-f42.google.com with SMTP id m15so399408uah.7 for ; Tue, 23 Feb 2021 07:26:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=PpY4KaTb3X6SL592gH6Z/hqSs3pvMGJYCa6I/q2Qhm0=; b=r+ztq+i+VGlVs+8zG65fS6qsYTueeXTdhEZv30I6pAOJXB143RSWcoqqOv8tInOBCt bVDhQmUtkn1tDeDBPfbTXHRwhw3UcGdQ99MzNx2wgqnQJEaJUo8Mh3Yv4xosLQ/Tmtvr QCJ/MhYo6te1jJF5Uft1zLZRQmlc9y4KUPdt3bF3DJC/chtT14Qrx2M4NcttCTtlGjio sSNVAPdrOqMxOnwhSzWuKTCESCUjnho3n4ApRqQilMWpwYIhvzbsC55sFpn6SMWooM43 O+rX2rGD6/5g6Wnf0ySGrrlddz0pwuWLsG3jY9rUyNBNCOMXg404exBVjL7SRW1OM79/ 0/AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=PpY4KaTb3X6SL592gH6Z/hqSs3pvMGJYCa6I/q2Qhm0=; b=bccZMgOzda61v9sRUNLwmyxFWPGlT/eM9ls7ycPQku+72cnilfyFTTO1qeGQ7X/IMy rSQPZzXE+I5SvfR2A3hGYA8qplpG4emHCNc4rtsuz6CrHijlwWTbPmbWNV+G41Tppphm okLFz7JWiSv8rOtJVXa1Qx9IwJ75lt8nK2v13BhtYwMYSrjAYH4n5/O0bIKx6XnBCynD 7zbFtErP2t2uBn8Hu1hVPwxRfzBib1QMiIUlgWd5kFI4CEQr56mGaQdY4aTtm6calE4z +bQrcZQuGJFIVYUXL/PEtejQ2P+j1itzBdfvsiRMWdnXyOm07v+kr+D8JePkE7YE4Hmx XEeg== X-Gm-Message-State: AOAM5324iIQTvR1wj+6h9VLTQcd1J1gKeR3Lrj1aNX+QcMHCA6YqdcRN 4iNnifCkWcl1NjJ0hrzoGjesARL/wdEhRfteL6M= X-Google-Smtp-Source: ABdhPJyVBgWi+I1fQ0LcrTqEUTqcjsKgTLUurCRDKLDEtaM83EZRkZC4uCXIYjsaeaJ1Du1kVN5gmo9mhLou9Gz4fmY= X-Received: by 2002:ab0:4024:: with SMTP id h33mr17301436uad.1.1614094003581; Tue, 23 Feb 2021 07:26:43 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 23 Feb 2021 10:26:32 -0500 Message-ID: To: Nikita Popov Cc: David Rodrigues , PHP Internals Content-Type: multipart/alternative; boundary="000000000000b7b6d205bc028b42" Subject: Re: [PHP-DEV] Inline conditional that returns null if falsy From: chasepeeler@gmail.com (Chase Peeler) --000000000000b7b6d205bc028b42 Content-Type: text/plain; charset="UTF-8" On Tue, Feb 23, 2021 at 9:31 AM Nikita Popov wrote: > On Fri, Feb 12, 2021 at 8:25 PM David Rodrigues > 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 chasepeeler@gmail.com --000000000000b7b6d205bc028b42--