Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113482 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 8759 invoked from network); 11 Mar 2021 17:12:35 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 11 Mar 2021 17:12:35 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 37DFA1804F6 for ; Thu, 11 Mar 2021 09:05:06 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f196.google.com (mail-qt1-f196.google.com [209.85.160.196]) (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 ; Thu, 11 Mar 2021 09:05:05 -0800 (PST) Received: by mail-qt1-f196.google.com with SMTP id 73so1661176qtg.13 for ; Thu, 11 Mar 2021 09:05:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=VEjm/Vb/qkbpnLHRb8qOG0m1eb48ctStERarTlI4Zno=; b=v/0M74u7H5yOa8evt8IKQbeEj2O7QiXuUGX09E4B7p5jFaZ3QqTFGT1wrtrG6X95k5 5ecbT+q9M5Luzxv/GvWNj1cdoZdn/dOgGT3U38STSQh0+Li+q8qg6tiBANpq4u+IJkyE RWlBs1Y6MCX8aejjUFu0pmb/isAtsDsTEAghtFr24SKdF/UGJk1QK1paKL2uM6tVdNmy zpUpP4bxuMQbgrgdB5qfzna7CSwGtSQMYySIns4GBi5xV+T6Q4HprvYOXYmCiRzri5HQ oGE7WpuGdw8nHii5Fi3vyMs+H1/+EGL4sMtrCfd8mrp0OKdQZSnqM6L21JTFY/NnzWbE tM0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=VEjm/Vb/qkbpnLHRb8qOG0m1eb48ctStERarTlI4Zno=; b=q94D0gFJdkIPiBmQ7nWgWxtNTEkqQNMUv5e+N+2uVsJjfW2qtAzhUVJ0v2i7IES8IR ogtNIInY0ssgAw45Zj1ldz7amo4tQO3isPqlXRPZzHLMyoQNsmqe4ub3CbocjSt0BSw0 OtrOmpbu6aaEoW7067/nXlsvY4mKJsnIJu00iMcuZ7DpJ83rhGU7ivPJWF2w4a5Es+Y8 u6NWUgXamVeOzsgTKagenm5I4CQHss4kfTmRk6uBjZGlXdYTG0u4tcYCywdd2H6RFYRh GMUtK8z5/smcyxRjKou2NgGMSCb5YFP8WMs90UtsxcuVHbhmOU9RtW9rj0YNjHiDvlR9 G9hQ== X-Gm-Message-State: AOAM530U9/SKtx0zfU1Q+/Kx6Vyr3VolhEPxPpidqY7iPEseZvr9Y7of 9CCAR47SNsh6BULnfmNlLQmtww== X-Google-Smtp-Source: ABdhPJwcbFt3rlMx1A1PYjV8bYCz5HtxRhvCRZYBIK+q9naICqMuCJx6z9L/KIUWqTKdzKYzGsV5Uw== X-Received: by 2002:ac8:149a:: with SMTP id l26mr8045723qtj.210.1615482301767; Thu, 11 Mar 2021 09:05:01 -0800 (PST) Received: from [192.168.1.239] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id 15sm2099965qty.65.2021.03.11.09.05.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Mar 2021 09:05:00 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) In-Reply-To: <01fa01d71627$edb078a0$c91169e0$@gmail.com> Date: Thu, 11 Mar 2021 12:04:58 -0500 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <5ACB32D5-C7EC-4332-ABB1-B519CEDCC019@newclarity.net> References: <01fa01d71627$edb078a0$c91169e0$@gmail.com> To: office.hamzaahmad@gmail.com X-Mailer: Apple Mail (2.3608.120.23.2.4) Subject: Re: [PHP-DEV] Honour the Default Value of 'return' Statement According to the Function Signature From: mike@newclarity.net (Mike Schinkel) > On Mar 10, 2021, at 10:37 PM, office.hamzaahmad@gmail.com wrote: >=20 > Hello Internals, > I congratulate you for the successful release of PHP 8.0.3. I also > appreciate your efforts to bring union types and return type hints. > Although the type hints have made debugging more easier, it has = created > another problem for those that use 'return' to end the execution of a > function or method call. Welcome. > There are mainly two usages of 'return'. First ends the execution of = the > remaining statements. Second returns a value. The return type hints = aids > with the second usage and ignores the former facility. In other words, = if > one wants to avoid large code blocks i.e. the conditional blocks, it = can > write like following. >=20 > function foo() > { > If (true /*condition1*/) > return; >=20 > if (!false /*condition2*/) > return; >=20 > /*the remaining part of a function*/ > }; > ?> However, I agree with Larry. This feels either like a code smell and/or = could be handled with a variable. This pattern can be very frustrating in other people's code when I = either need to debug it or might want to debug it to learn the code = because it requires setting IDE breakpoints in many places to be sure = that catch an exit. And invariably I miss breakpointing a return when = debugging and am left wondering what happened. See = https://youtrack.jetbrains.com/issue/WI-40862 As an alternate I developed a technique using `do{...} while(...)` in a = novel way that I wish PHP supported more cleanly, but it nonetheless = works great.=20 The technique uses `do {...} while(false)` to create a block that break = can exit early on guard clauses, for example (btw, I would return `null` = instead of `'unknown'` and enclose the breaks in braces for the = `if(...)` statement, but I wanted my example to be equivalent to yours): The above function returns null if conditions are met. Otherwise, it = returns > no value. It was okay when there was no concept of return type hints. = When > return types have been introduced, the behaviour needs to be evolved, = too. > What if the return type was set 'int|float' in the above example? The > compiler would give following error: > Fatal error: A function with return type must return a value.... = Similarly, > a function with ': void' signature produceed that a function may not = return > a value. > To resolve such a problem, I request to honour the function signature = when > you evaluate return statements. Instead of looking for no 'return' > statements in ': void' functions, verify whether a function specifies = a > return value. If the function returns a type other than the types = declared > in the signature, it may produce wrong type error. If a function with = void > returns a value i.e. contains a non-empty return (return;) statement, = the > compiler may throw an error stating that a function with void = signature must > not return a value. > Likewise, 'return' may return an empty array if the return type was = set to > array; the similar case applies to string, int, float, object = etcetera. In > the case of ': void' signature, 'return' only ends the execution and = returns > no value. > PHP also allows to specify only returning 'false' from a function = call. What > if you extend this functionality and allow specifying literals = including > null? If nulls are also allowed, the backword behaviour could be > impleamented. For example: > public function foo() : string| null; >=20 > Consider the following example for literals. > function get_nationality_string(int $country_code) : string | = "unknown" > { > return; > }; > ?> >=20 > I am positive that you will consider my request and provide me with = the > feedback in case of any inconsistency in my argument. I wish you best = of > luck for the on-going work for the PHP 8.1. > Best Regards > Hamza Ahmad >=20 > --=20 > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >=20