Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113220 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 72854 invoked from network); 23 Feb 2021 14:43:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Feb 2021 14:43:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7EBD61804C3 for ; Tue, 23 Feb 2021 06:31: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=0.6 required=5.0 tests=BAYES_50,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-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 06:31:46 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id z11so11456198lfb.9 for ; Tue, 23 Feb 2021 06:31:45 -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=hb1lO1ovJKI1BP4uHxuJwl60hpiRDD/LqNRE9NN6JhA=; b=CrAgqQO6xdIvguSImTww/PJIdbr1lj6Ke0QjaRPzCkoUJh0Op0GHxlrcf8v1NtqZIy kBVT9wHCZS8dzFnrh51tPcHrYP8Ll4XhdCx1Ghn3u4tvCVTF+eUA/oD9U2lGqOo4KQuv nr3VKAtilaUZnWTopmZ8kEOddbh1rKMQeg1del0HdqxqP4wDTE4vI0EfMb/4KqJgLIYH bFAE7uuyFc1afiX4w+wKVe7J/BIADzGz5cT0L93VdBIAG1RbnXAwrhnTz5lir0ZiJXu5 IGyqgYQM/eKHsHIx+0FqV5z3aqH2H08b6owQewcEjD2tT0CcyQmZz4xDpk+03HGgGBfl l4Wg== 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=hb1lO1ovJKI1BP4uHxuJwl60hpiRDD/LqNRE9NN6JhA=; b=BpCbyn6hQ10OQDdpbo86c3DGh9J+7WlxCW7bseyPi7iz3/hyPE3tU/OX9R5RHGT/lT mBux9/Sz6SHswFJB0Xc/7RWfbw6Rjme90TMUxPqOccSXK8AK3os88EyOQ2AnXxwS6B+i mPpUUmcJSpWVjq8yqn5SGKL9tRJ9zA/MvuVT4rwmcKyFR2gziOwv/I/9Prg9u3+CEEzq Thmkp1Q8MLxFtnlAixTiekYlyHMYvV8QB4KFCR9RzvskHHDZXCmGsHpAqoGkjhxqw2x/ OVshq4K7iWM7MG+0LkOox9QVemwEtEtlv7a8hU6kRr4AtmDmMRZeJmGYiBbtP1i2S1Yu sigQ== X-Gm-Message-State: AOAM5304JDUTxXoYGm3E7DHGm2SZzMgQmnnv4+idOazKti59qyHDrKvO 3+kcgh5zMDA8iYa+Atob5Pu8VMkSft7nmyuYfNs= X-Google-Smtp-Source: ABdhPJwr8/XWeDROXYwxipbFTJiH+FDncEFrsRJ0+R61SVv/v83E7AqxiXhdRnIIZkF3lBTZp45gbcU5SvJUIYPDF+4= X-Received: by 2002:ac2:4c32:: with SMTP id u18mr10828985lfq.223.1614090702669; Tue, 23 Feb 2021 06:31:42 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 23 Feb 2021 15:31:26 +0100 Message-ID: To: David Rodrigues Cc: PHP Internals Content-Type: multipart/alternative; boundary="000000000000f7cb6005bc01c695" Subject: Re: [PHP-DEV] Inline conditional that returns null if falsy From: nikita.ppv@gmail.com (Nikita Popov) --000000000000f7cb6005bc01c695 Content-Type: text/plain; charset="UTF-8" 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. Note that => cannot be used for this purpose, as it is already used for array literals. Regards, Nikita --000000000000f7cb6005bc01c695--