Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113490 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 44275 invoked from network); 12 Mar 2021 02:54:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 Mar 2021 02:54:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9F7271804B8 for ; Thu, 11 Mar 2021 18:47:01 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 18:47:01 -0800 (PST) Received: by mail-wr1-f41.google.com with SMTP id u16so3975172wrt.1 for ; Thu, 11 Mar 2021 18:47:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:references:in-reply-to:subject:date:message-id :mime-version:content-transfer-encoding:thread-index :content-language; bh=ZJKQGMjC/VC8g1TInexkA4WAXCwYHzNbQExjoyo2vqw=; b=ePMnMTBRP/gFUDn0ENJ7gR94s4RsL6zCcrWQdRTNAvK5LFEQnMtzFphMTP42h7Cw62 G0cJ3xqwSQzESChVjqnYbUQ83/jdzil2uliSbPIxWM7SxGEkk1IUiD3lrzrFQxr8KiAq mLEIYgo1kAQBbZQS6/l0XjrnICI6wnVcY3SReB6GdaNyP0mmODwj4xRZdrq79dOyc/KO U7j307FekEzmFvRR1dMK7pNDqCPk03XZhl0IjhylKOyp8HYCROA6dhRGIrSPNF3MwZ8f u/ebpwQM16jF4JnOBgN+o94l/Rx0BW9y2MjSJsHnkUbDmo6Tz+oZrlRpHfK4g1/6gg6m LOJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:thread-index :content-language; bh=ZJKQGMjC/VC8g1TInexkA4WAXCwYHzNbQExjoyo2vqw=; b=GyZ1wQUSjDqGnbPEEE0GdnLlvCSkGJoFQ6uNWDUQXXWY93CW+JyNRBR74FKP2s5siL VhvHyTJpp2MC4BDPFhe40GrWcuhfmWbeps9qXi+cii5WH9I1YtJDd1PgECSioe17YROr GioHAepvtWmxuiAlEvaruX66Y4GDeRmtalNc6rdCYoH/iztFONOlVxvVy2Pn7ZYdDYv5 FSqrNd4JMR6lS6EhnryfbgzczpHqM+dTcKnW93woFSJkV9DyRxzwx3eKjCGCqJj7TKr4 DKsaYCAHLnx7fAxQy8/6IZdLM5f9mqP9QTcnx3D7GvIkfoIFcQxIHT2KWFDMF3SeuMsK icKA== X-Gm-Message-State: AOAM530/ksJlqhnLIN695Aw+ii8Nxj+L7mNW1/Ktbzr+NSyQ0iVmVS5h de2WoK0ptEUFhNfIT13YPoGIE0FL4bPrnqWP X-Google-Smtp-Source: ABdhPJwQAyZ9RUqxM+efkruA4v6kQxFYj55ta7QrYfsZEO3dijKqEIYohhWlBaYBn1sr8t7pCeutXw== X-Received: by 2002:adf:a2c7:: with SMTP id t7mr11722934wra.42.1615517219663; Thu, 11 Mar 2021 18:46:59 -0800 (PST) Received: from DESKTOPQD1QNOQ (static-84-13-248-43.dnt.net.pk. [43.248.13.84]) by smtp.gmail.com with ESMTPSA id w6sm6279669wrl.49.2021.03.11.18.46.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Mar 2021 18:46:58 -0800 (PST) To: "'Mike Schinkel'" Cc: References: <01fa01d71627$edb078a0$c91169e0$@gmail.com> <5ACB32D5-C7EC-4332-ABB1-B519CEDCC019@newclarity.net> In-Reply-To: <5ACB32D5-C7EC-4332-ABB1-B519CEDCC019@newclarity.net> Date: Fri, 12 Mar 2021 07:46:54 +0500 Message-ID: <002301d716e9$f971ba10$ec552e30$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQEkxYUdW5hh6aEiWom8mpkeVucJUAGYmSYJq9eAZ0A= Content-Language: en-us Subject: RE: [PHP-DEV] Honour the Default Value of 'return' Statement According to the Function Signature From: office.hamzaahmad@gmail.com ("Hamza Ahmad") Thank you for providing me with the feedback, experts! Initially, I = wanted empty returns in void functions. When I prepared the request, I = included the other solutions that I felt important and worth adding. Is = it not possible to go with empty returns in void functions? I agree that = the code can be adjusted to fit the current environment; still, people = avoid writing lengthy blocks of code. An empty return with defaults will = help them with the code that is easier to maintain. I clarified my stance on first email in a response to a respectable = member of the list. I do not know why that email is not appearing on = externals. I will make sure that next time I post an external URL that = contains the changelog and do not clutter the mailing list. I am again thankful to you all! Best Hamza Ahmad -----Original Message----- From: Mike Schinkel =20 Sent: Thursday, March 11, 2021 10:05 PM To: office.hamzaahmad@gmail.com Cc: internals@lists.php.net Subject: Re: [PHP-DEV] Honour the Default Value of 'return' Statement = According to the Function Signature > 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=20 > appreciate your efforts to bring union types and return type hints. > Although the type hints have made debugging more easier, it has=20 > created another problem for those that use 'return' to end the=20 > execution of a function or method call. Welcome. > There are mainly two usages of 'return'. First ends the execution of=20 > the remaining statements. Second returns a value. The return type=20 > hints aids with the second usage and ignores the former facility. In=20 > other words, if one wants to avoid large code blocks i.e. the=20 > 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=20 > 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=20 > compiler would give following error: > Fatal error: A function with return type must return a value....=20 > Similarly, a function with ': void' signature produceed that a=20 > function may not return a value. > To resolve such a problem, I request to honour the function signature=20 > when you evaluate return statements. Instead of looking for no = 'return' > statements in ': void' functions, verify whether a function specifies=20 > a return value. If the function returns a type other than the types=20 > declared in the signature, it may produce wrong type error. If a=20 > function with void returns a value i.e. contains a non-empty return=20 > (return;) statement, the compiler may throw an error stating that a=20 > function with void signature must not return a value. > Likewise, 'return' may return an empty array if the return type was=20 > set to array; the similar case applies to string, int, float, object=20 > etcetera. In the case of ': void' signature, 'return' only ends the=20 > execution and returns no value. > PHP also allows to specify only returning 'false' from a function=20 > call. What if you extend this functionality and allow specifying=20 > literals including null? If nulls are also allowed, the backword=20 > 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=20 > the feedback in case of any inconsistency in my argument. I wish you=20 > best of luck for the on-going work for the PHP 8.1. > Best Regards > Hamza Ahmad >=20 > -- > PHP Internals - PHP Runtime Development Mailing List To unsubscribe,=20 > visit: https://www.php.net/unsub.php >=20