Newsgroups: php.internals,php.internals Path: news.php.net Xref: news.php.net php.internals:110209 php.internals:110210 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 92294 invoked from network); 19 May 2020 08:15:59 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 May 2020 08:15:59 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 138FA1804C2 for ; Mon, 18 May 2020 23:54:22 -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 ; Mon, 18 May 2020 23:54:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589871258; cv=none; d=zohomail.com; s=zohoarc; b=lgXOe4u5FIuq/l5w+ZNXfkSgYNzwY/iOLguzPsu4K4/XIQx2+EtFwiXnM3qfWYqnbrmYoJ5eLoaYH/HxhJ3KF48ypmabV72wEQQ6kj4brxMYhlfrYRZmOI/ptJcLdskldFbGEuZQonDiA6E7vaen7EhskeomDLhxqLYB/VTP5VA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1589871258; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:To; bh=MQAKqX0stARrLwJk443G2N9ZhY7TJi3EZCglBCab6Qk=; b=jLamJdKjHtjzDOG+4kfwB3Ui/+1uNBct8j7i97VS5FdYcc35pV5AaxhZrBtuefLO8jXn+C8DaoG8KJaGR7zDf3kveAEOIAHg2scSAqBSoHZ6WV0CV5yr0IOeIuVI10I70+XMTda27W2fErl4KqWH5XMZpPzmfVnvo1Y8WhsYu+M= 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=1589871258; 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=MQAKqX0stARrLwJk443G2N9ZhY7TJi3EZCglBCab6Qk=; b=XV1UDH4Xp8JIZBrOfSdFdPBNpitWolGjPFUqIFpQq+faZmDjEWrU/cetlLkVt+S/ N1rdcYR+BrtAaaLZCsqzwckL0P52d2Rb0qjbXbNndwhjKpho2UvrCrqTi4QCmDPcK5T 9Ca4iVzovf8v5v0S1QOx6FiqV7aOe1q4+adS/ALI= Received: from mail.zoho.com by mx.zohomail.com with SMTP id 1589871226557773.5290494083353; Mon, 18 May 2020 23:53:46 -0700 (PDT) Date: Tue, 19 May 2020 08:53:46 +0200 To: "Peter Stalman" Cc: "PHP internals" Message-ID: <1722bb5aeba.efaf5e2a253261.3998926174950017220@manuelcanga.dev> In-Reply-To: References: <85C8412B-B04A-4853-8C4C-E270FCB35EEE@getmailspring.com> 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) Hi, Internals, ---- 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 separate > 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 > function foo($bar) > { > if ($bar =3D=3D=3D 1) > return >=20 > if ($bar =3D=3D=3D 666); > delete_everything(); > } >=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 think > 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 longer > tell if a return is actually a return at a glance. >=20 >=20 > Thanks, > Peter >=20 I agree.=20 =C2=BF Maybe something like... function main_function() { escape when( calculate($var) ); /* do other important stuff */ } `escape when( expr )` returns value of `expr` to caller function when `= expr` evaluate to true otherwise next line. function main_function() { escape with $ a + 1 when( !calculate($var) ); /* do other important stuff */ } `escape with expr1 when( expr2 )` returns value of `expr1` to caller funct= ion when `expr2` evaluate to true otherwise next line. Regards -- Manuel Canga