Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117370 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37840 invoked from network); 18 Mar 2022 09:20:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Mar 2022 09:20:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 44E1C1804D0 for ; Fri, 18 Mar 2022 03:46:18 -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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (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:46:17 -0700 (PDT) Received: by mail-wr1-f53.google.com with SMTP id b19so11146049wrh.11 for ; Fri, 18 Mar 2022 03:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=ezriIVd/JHuFHnEl21T+LVDuwa6Vj8zPWP3JHgAG4Ys=; b=Buk0aKPx2ZPrySM+Wu6iBnq2wtxrT5CHXbtJKkT8zip/JP3y4m25Uuc17O8IIk5pC9 rSWl6PjfCqWMbZIpyqJPwhhrJIip6PhEX7d7RmMUg1CPP9sW5PrWbub1Ulp8qG3nF5Lz OAqlmrV2igkmerG1ZPGAfaxqMZ18utkUNlrbu86Qx0HdMqcXAe1tq0e42DqgQ2vD9ynS Hm727yX+EEhDyiiC+GAaxOSz/r7J2NymywS+nDylowaQGLmwsPlkVPgusTNfNTiutieH KGZeXMSIFvI5z/bmcxCOxUIPZ0fx7ASc508PGhVL9GHcIEA4xJZ4efvFjONGAWsVvbGh CnPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=ezriIVd/JHuFHnEl21T+LVDuwa6Vj8zPWP3JHgAG4Ys=; b=LTMEpC+olWsn+P9LVR9106mgdLgYsLCOV8PGrRwF83ki3k8lXo0RLvIM/PnGOnxymo denE1XmptvzRA+fTzd6NGPoYyfPDi1J4MynATay1zcGNXXHT/G7Ajzv/JXFy/3jbo8C9 dwyQmWh1+kyrZEc7Day5WywxubO+oiNMjiA4mX4tqirx777d93RCZ68FxIK+oC4yV+xH 9vEs4Ua0H/fiH0HcRpJZQVUxhIhB8Epb7QXjEQqlN8gk+cZoDd1t8H7kfXYycTVuRoru hDSyYCufANqtibqsHAAb8qREmkFi2yLCA1P7UTr9y5MiLZr8fFxpQQctr1YxIWhWJDDr kMBA== X-Gm-Message-State: AOAM531514hmKNtIpgT7i7tDFW28zDfv1YVx8ApUWo9YecD564Z0TqHo BkZwTc/QvRBQ9SrM0K1i4eEbIGYbwp0= X-Google-Smtp-Source: ABdhPJzp/LE6YfegEW2ntOUHFC4bA6bj4AE23WcgL693GTHxM6TP3kHBE10cvalvJpPpCzFc5XCeLA== X-Received: by 2002:a05:6000:1841:b0:203:a3ed:7aec with SMTP id c1-20020a056000184100b00203a3ed7aecmr7395406wri.180.1647600376526; Fri, 18 Mar 2022 03:46:16 -0700 (PDT) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id g10-20020adfe40a000000b00203eb3551f0sm4756357wrm.117.2022.03.18.03.46.15 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 18 Mar 2022 03:46:15 -0700 (PDT) Message-ID: <7bf8a063-a8b9-f508-9d88-0ce9fe9a26d9@gmail.com> Date: Fri, 18 Mar 2022 10:46:15 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Content-Language: en-GB To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] Convert return to expression like throw From: rowan.collins@gmail.com (Rowan Tommins) On 18/03/2022 10:18, Florian Stascheck wrote: > function getUnreadDocuments(): int { > $documents = Auth::user()?->getDocuments() ?? return 0; > // actual logic of the function > return 42; > } I think I'd probably reject that example as too cryptic in a code review; the mixture of assignment and control flow is hard to follow, even more so than this, which is already possible: if ( ! $documents = Auth::user()?->getDocuments() ) return 0; I can imagine some people would like it, though, particularly those used to the Perl "doSomething() or die;" idiom. The throw expression has uses in things like lambdas and match expressions, which only accept expressions not statements. I can't think of any examples where "return" would be useful in such contexts, and in fact wonder what they'd even do: return $foo = match(1) { default => return 42; } $foo = fn() => return 42; function foo() { return return 42; }  // presumably you could stack this endlessly, "return return return return 42;" function foo() { yield return 42; } Throwing exceptions already aborts the control flow, so doing so in the middle of an expression is easy to understand; having an early return in the middle of an expression is harder to understand. Regards, -- Rowan Tommins [IMSoP]