Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110211 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 98307 invoked from network); 19 May 2020 08:26:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 May 2020 08:26:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 323EE1804C7 for ; Tue, 19 May 2020 00:05: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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS2639 136.143.188.0/24 X-Spam-Virus: No X-Envelope-From: Received: from sender4-op-o11.zoho.com (sender4-op-o11.zoho.com [136.143.188.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 19 May 2020 00:05:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589871915; cv=none; d=zohomail.com; s=zohoarc; b=ZGwBOp4N+xx5O/RF7t6POKUpas7dgitW0eWnzYQPTGXeNebC2OjlSuA0c+AxLS7Z0dp2l+PJcw6IqsO9NNcD3cTjNvoEd5mZ1g/+mtYCvPp9xnCHNjNH0yEFJjAUzGYbyGBefYJ4JHeunLmLlLgkHIwJaG/OS5gOfwuht8IyesU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589871915; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=x2ElOi8t3iafqCR5UpX7Jb0Nr9JanZeIdJKYvU9Z7ww=; b=K85XPuWbXlJwxzfaAa2qfC1Aswsx8AZUZ6ff9plVYgwo34GZDmTqoVYGFoluTOcUnTPH4Y94aPZa3247HbBPxLEk0ZEeWIt5R6a+bVyOHKJTMOyS1sVCqeDrCvgLF7wqfgK1WTe6J4N3Mmf0Q8h/aFDB8kJsCw4wX9ydR/GAkag= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=manuelcanga.dev; spf=pass smtp.mailfrom=php@manuelcanga.dev; dmarc=pass header.from= header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1589871915; s=zoho; d=manuelcanga.dev; i=php@manuelcanga.dev; h=Date:From:To:Cc:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=x2ElOi8t3iafqCR5UpX7Jb0Nr9JanZeIdJKYvU9Z7ww=; b=BYJM4vFa/HHpvw97MqDMwS70ViJETXHDPQW2g8amdGqMnQzvJbJDjV0apMCt8O3O nCXFsydhGVo++w/KZut9dlhW61llDWxVd3NEKoUj+QeJ1HB9KZxp2EHhblOzj/er6Nd qzdhoV+/kmhanbVQCv0A9Nhict1YTTANAAnSefsI= Received: from mail.zoho.com by mx.zohomail.com with SMTP id 1589871883455154.13080917762636; Tue, 19 May 2020 00:04:43 -0700 (PDT) Date: Tue, 19 May 2020 09:04:43 +0200 To: "Peter Stalman" Cc: "PHP internals" Message-ID: <1722bbfa250.1200d3c6a254667.725500817558436494@manuelcanga.dev> In-Reply-To: <1722bb5aeba.efaf5e2a253261.3998926174950017220@manuelcanga.dev> References: <85C8412B-B04A-4853-8C4C-E270FCB35EEE@getmailspring.com> <1722bb5aeba.efaf5e2a253261.3998926174950017220@manuelcanga.dev> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Importance: Medium User-Agent: Zoho Mail X-Mailer: Zoho Mail Subject: Re: [PHP-DEV] Proposal For Return-If / Early Return / Guard Clause Syntax From: php@manuelcanga.dev (Manuel Canga) ---- En mar, 19 may 2020 08:53:46 +0200 Manuel Canga = escribi=C3=B3 ---- >=20 > Hi, Internals, >=20 >=20 >=20 > ---- En dom, 17 may 2020 06:33:51 +0200 Peter Stalman escribi=C3=B3 ---- > > A few thoughts: > >=20 > >=20 > > 1. I agree with the sentiment that this syntactic sugar doesn't > > actually save any verbosity, so it's not really syntactic sugar at > > all. > >=20 > >=20 > > 2. There appears to now be another RFC by Pavel Patapau, specifically > > focused on a Guard statement as a new keyword > > (https://wiki.php.net/rfc/guard_statement), which now has its separat= e > > discussion. > >=20 > >=20 > > 3. As Thomas Lamy mentioned, I too would prefer a single keyword. > > Consider the following two examples: > >=20 > > function foo($bar) > > { > > if ($bar =3D=3D=3D 1) > > return; > >=20 > > if ($bar =3D=3D=3D 666) > > delete_everything(); > > } > >=20 > >=20 > > Both would now be valid syntax, and IDEs would have a harder time > > warning about the misplaced semicolon in the second example. Wouldn'= t > > be very common, but still. > >=20 > >=20 > > 4. However, this RFC is interesting to me because there be a way to > > modify it to allow for less verbose refactoring, and kinda allowing > > returns to bubble up like exceptions do. I think it would only make > > sense if it's a "if not null then return" type of thing. > >=20 > > Consider the following (a bit contrived, but I hope you get the point= ): > >=20 > > function main_function() > > { > > $result =3D calculate($var); > > if ($result !=3D=3D null) > > return $result; > >=20 > > /* do other important stuff */ > > } > >=20 > > function main_function() > > { > > ifnotnullreturn calculate($var); > >=20 > > /* do other important stuff */ > > } > >=20 > > Obviously not an ideal keyword, but this is the only thing I can thin= k > > of where this type of syntactic sugar makes sense and decreases > > verbosity. Something similar can also be accomplished with exception > > though. > >=20 > >=20 > > 5. Finally, I think if we start putting several returns at the same > > indentation then the cognitive load increases because we can no longe= r > > tell if a return is actually a return at a glance. > >=20 > >=20 > > Thanks, > > Peter > >=20 >=20 > I agree.=20 >=20 > =C2=BF Maybe something like... >=20 > function main_function() > { > escape when( calculate($var) ); >=20 > /* do other important stuff */ > } >=20 >=20 > `escape when( expr )` returns value of `expr` to caller function whe= n `expr` evaluate to true otherwise next line. >=20 > function main_function() > { > escape with $ a + 1 when( !calculate($var) ); >=20 > /* do other important stuff */ > } >=20 >=20 > `escape with expr1 when( expr2 )` returns value of `expr1` to caller fu= nction when `expr2` evaluate to true otherwise next line. >=20 >=20 Upgrade version. Maybe is better: `escape when( expr )` returns null to caller function when `expr` evalua= te to true otherwise next line. `escape with expr1 when( expr2 )` returns value of `expr1` to caller func= tion when `expr2` evaluate to true otherwise next line. Then these code are equals to: > > function main_function() > > { > > $result =3D calculate($var); > > if ($result !=3D=3D null) > > return $result; > >=20 > > /* do other important stuff */ > > } > >=20 function main_function()=20 { escape with $result when( $result =3D calculate($var) ); /* do other important stuff */ } > > function foo($bar) > > { > > if ($bar =3D=3D=3D 1) > > return; > >=20 > > if ($bar =3D=3D=3D 666) > > delete_everything(); > > } function foo($var) { escape when( $bar =3D=3D=3D 1 || $bar !=3D=3D 600 ); delete_everything(); }