Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117369 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 35570 invoked from network); 18 Mar 2022 08:53:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Mar 2022 08:53:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5F7C71804D0 for ; Fri, 18 Mar 2022 03:19:09 -0700 (PDT) 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.2 required=5.0 tests=BAYES_40,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, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f177.google.com (mail-yw1-f177.google.com [209.85.128.177]) (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 ; Fri, 18 Mar 2022 03:19:05 -0700 (PDT) Received: by mail-yw1-f177.google.com with SMTP id 00721157ae682-2dbd8777564so86724567b3.0 for ; Fri, 18 Mar 2022 03:19:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=8s8apI/8Aqk2fCejAWPHDx4FbKnuBu+S7ofPXZLot1U=; b=Mo71h29vV87Fppw1fvLTCf8XWdnVgRaxsf4iryUpO0+LacZEajXHx+etCbirZZHxka SYnsocjk1kQTQmTShk1BmV76gpyFGsuf04LZWN4sNpnDwcrenI9E8/+5k7ZfMjxCU7If e7Xd6lDsTiTsnxChGNKYOttcqTqW9HWb6VMfIuL+LV2zQBsD94yZsqyCrLZzF00XplWI pjViZ0YVhQFiLSf7fzZCEdJnWtX8G+pMXQFSTHCn793+ftKZtLiFQ+f2+itJfTVg3AWE 0RekXrK9lsaTFhJwRZKhPirAFJeK9rB3d/DW/Q0kgyZ45UDwXoHIc3BC1tiEgvPozcSY mtPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=8s8apI/8Aqk2fCejAWPHDx4FbKnuBu+S7ofPXZLot1U=; b=DGnpzkaRa2RPL5Ymi7rvsLj+fJYhFk9WTCUrAxpPu2wfh0236u/4fRp7/U00GhokuY ywBnmTlJzNo+GbSYmuUlQ8QWuct81bMi0goj/fbI5qTDlOmfDAptv3QOUYRlBj88AAe/ POygTEEZItFoaI0fKTdDi3L9xn2rx8jgAZs5XtYiNNubYtgM97eLR5pW/dhAwxC4g3Pc zRxeRygp6wtKO6p91RtgzHnvFgosCYCrfIKbRekVpyDrVgdFMxyD91/naNqEfI79cBH5 ToIvNZQgKiDvQij6B5ScGRf+OeGzzwxPjR+nCKw+qh2ZrcnWXa6n+TkZXy5OM7BclhZZ A1VQ== X-Gm-Message-State: AOAM531RSl7RfBlVyPUKiiEWRavhAeZZ5ir9vsmNkeFupPyPDPnmE22N Nr85PwKSaIsa4yVGOxDemyib9DjgeaTNHbxOAoczjoVx3BM= X-Google-Smtp-Source: ABdhPJy+RJhgximV0vWTFmmkMvYMUAyZjFhAQHiDacYTxiqc08uyTu1kfWZQE31R8yMVBmOgDzcUHHEBzE5t/v50Q58= X-Received: by 2002:a81:fe06:0:b0:2e5:6db0:bcbc with SMTP id j6-20020a81fe06000000b002e56db0bcbcmr10380720ywn.337.1647598744909; Fri, 18 Mar 2022 03:19:04 -0700 (PDT) MIME-Version: 1.0 Date: Fri, 18 Mar 2022 10:18:53 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000ec382805da7b799e" Subject: Convert return to expression like throw From: florian.stascheck@gmail.com (Florian Stascheck) --000000000000ec382805da7b799e Content-Type: text/plain; charset="UTF-8" Hello, We remember the old days, when we had to do: function getUnreadDocuments(): int { $user = Auth::user(); if (is_null($user)) return 0; $documents = $user->getDocuments(); if (is_null($documents)) return 0; // actual logic of the function return 42; } And it was already really nice to do: function getUnreadDocuments(): int { $documents = Auth::user()?->getDocuments(); if (is_null($documents)) return; // actual logic of the function return 42; } But I think it could be written even more concise with just reusing existing syntax elements: function getUnreadDocuments(): int { $documents = Auth::user()?->getDocuments() ?? return 0; // actual logic of the function return 42; } This is exactly the type of change that PHP 8.0 introduced for throw ( https://wiki.php.net/rfc/throw_expression) and to me it feels very natural to write code that has this change also made for the return statement. In the example above, depending on developer preference, the same result could be achieved with " || return 0;" instead of the ?? operator. Was there any previous discussion about this and was there maybe a good reason it hasn't been implemented? I couldn't find anything when searching. What are your thoughts on this? -Florian --000000000000ec382805da7b799e--