Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113232 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 96368 invoked from network); 23 Feb 2021 17:01:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 23 Feb 2021 17:01:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AAA62180541 for ; Tue, 23 Feb 2021 08:49: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=-1.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,FREEMAIL_REPLY, 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-vs1-f49.google.com (mail-vs1-f49.google.com [209.85.217.49]) (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 08:49:42 -0800 (PST) Received: by mail-vs1-f49.google.com with SMTP id f26so8774899vsr.0 for ; Tue, 23 Feb 2021 08:49:42 -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=x3+/eMx82AsPFXYYhHKNDcXgwOiSyVdNGedxo0kX8k4=; b=u9qCZ9g8o8QTYb+EEeHeX0rBifXDWP82zWB5pvu+KGSmEs7D9SgQHZyvbAlDwCg7sV sODQzrqMju4+HTy3sqzFjK8mMsV8os7DNw5e/LeDwaOwNWtRQiNRIDbnDfBYnLDqCtjs 5t/o2d91fnRWKVZVLeeUEmbcuVUB/B0hRtd9kbcL9VB0G9lvvNEpeiJRJEfPu1SqR8ke 3G5YmBvz+gS+xUhc8Zj4Le74hjTs3PwkPq/GbhHZuU7vo4RAiazjhcZra5+FD5+ji5wd stXqR0LTS6Eiyj10yKv7DFmQ02JNHbxI1Ut7X1cs3SX7sa/L+gbor5J0e90DnYtERKXb nhgw== 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=x3+/eMx82AsPFXYYhHKNDcXgwOiSyVdNGedxo0kX8k4=; b=DlGh1SQ8utkgRLmCfYMg7+PPmO+junv7XWONJljYJRWNcnn0gtYEjZkbF0YHuPzAY9 uPT3OaABQbxJS/KVNj6K+WnYryVyVOd2KTToNtKMJlQTu08L3LqcGGeC7tfeRi+IWfmt 3OE45nQOf613HjqyrTBKYopyfPoMTbMYp3geT9aWuXE6LG0f5xmqC9ks4wv6KsNUKslX e9eFQVj+N2viZGnkMLi4q6FdxOQuO8cDypaOjQNED6lzOl1eLcYT5Pz3v83LeP4arxs7 GU4IAITTr5y798QZ8qijtv3Fzl4JnpRL47KNnOM0z6CnLI9lGBpfxMf4LInaUCPqyxzw Qwlw== X-Gm-Message-State: AOAM532zr8v/gpKWMwLT3QkK52ZJ4ExN6kr/8nMkCJw8tE+sdJ/NhP7j EAWWQ1L5IpahSWiqmkysFb3xsvwmsR8v2vmaESs= X-Google-Smtp-Source: ABdhPJxtWWeSWs8upht/HKuxb5vA4akSKIY+JhBUSkIfbGLYXDMiV9IEcODdCIKTHFbKpKrTSb3D23tNQI+IjUr4lbY= X-Received: by 2002:a67:fa96:: with SMTP id f22mr16535648vsq.45.1614098978716; Tue, 23 Feb 2021 08:49:38 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 23 Feb 2021 17:49:27 +0100 Message-ID: To: Chase Peeler Cc: Nikita Popov , David Rodrigues , PHP Internals Content-Type: multipart/alternative; boundary="00000000000042417a05bc03b425" Subject: Re: [PHP-DEV] Inline conditional that returns null if falsy From: albertcasademont@gmail.com (Albert Casademont) --00000000000042417a05bc03b425 Content-Type: text/plain; charset="UTF-8" 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 wrote: > 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 > --00000000000042417a05bc03b425--