Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109156 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 40474 invoked from network); 19 Mar 2020 23:47:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Mar 2020 23:47:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CB6BD1804D8 for ; Thu, 19 Mar 2020 15:10: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.1 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HTML_MESSAGE,MALFORMED_FREEMAIL,MIME_QP_LONG_LINE,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS714 17.58.32.0/20 X-Spam-Virus: No X-Envelope-From: Received: from ms11p00im-qufo17291301.me.com (ms11p00im-qufo17291301.me.com [17.58.38.42]) (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 ; Thu, 19 Mar 2020 15:10:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=me.com; s=1a1hai; t=1584655812; bh=Bb/4+jdiKzfNRqAjYBY3lw7yhqyK+krrsQzCybRDrQQ=; h=Date:Subject:From:To:Message-ID:Content-type; b=YGJZqf+BuAM8tNaoz4GciKYc0ea5ooQVS95jjo3a/M7SW+lnQIown1PhWsnPYympM jvirnefx4H0Vu32rvt6faAzSmw7ENN8wczwO5RXfzpW4wEDKdxyWu+1s++MxnTqLWP rH5eUJSwBf1dc46gMsGCe3d5YzC4AzWs+bu6g1bbAunhB1/oJ3KC4SPIz36HMQmvZs a20gPbz3j0icniAFJwZaMkczAsCLSI9pY4ZdA6Z60nQMqxyvYr1nOT++yHIIyuaiGJ 6G+Xr0fjdrcitMQdWjD99MLRf8mnZp3FuxLP6EfJ1FdI/Y3i3AV15o7qI7sNNlJHRx rX64ddUU6GSrg== Received: from [192.168.43.208] (197.226.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch [178.197.226.197]) by ms11p00im-qufo17291301.me.com (Postfix) with ESMTPSA id 87B1C100736 for ; Thu, 19 Mar 2020 22:10:11 +0000 (UTC) User-Agent: Microsoft-MacOutlook/16.35.20030802 Date: Thu, 19 Mar 2020 23:10:02 +0100 To: Message-ID: Thread-Topic: [RFC] throw expression Mime-version: 1.0 Content-type: multipart/alternative; boundary="B_3667504211_762001550" X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2020-03-19_09:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 mlxscore=0 mlxlogscore=657 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-2003190090 Subject: [RFC] throw expression From: ilija.tovilo@me.com (Ilija Tovilo) --B_3667504211_762001550 Content-type: text/plain; charset="UTF-8" Content-transfer-encoding: quoted-printable Hi everybody! =20 Yesterday I stumbled upon this Tweet by Nikita: https://twitter.com/nikita_ppv/status/1240309838950866946 =20 It answered why this seemingly correct block of code doesn't work: =20 =C2=A0=C2=A0=C2=A0 fn() =3D> throw new Exception('nope'); =20 The answer is rather simple. `throw` is a statement, not an expression. How= ever, arrow functions only allow a single expression as their bodies. The ob= vious solution is to convert `throw` to an expression. This would also allow= for the following: =20 =C2=A0=C2=A0=C2=A0 $value =3D $nullableValue ?? throw new InvalidArgumentException(); =C2=A0=C2=A0=C2=A0 $value =3D $falsableValue ?: throw new InvalidArgumentException(); =20 At the moment this is generally done by moving the `throw` into a function: =20 =C2=A0=C2=A0=C2=A0 function throwInvalidArgumentException() { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 throw new InvalidArgumentException(); =C2=A0=C2=A0=C2=A0 } =20 =C2=A0=C2=A0=C2=A0 $value =3D $nullableValue ?? throwInvalidArgumentException(); =20 or of course by just using an `if` statement: =20 =C2=A0=C2=A0=C2=A0 if ($nullableValue =3D=3D=3D null) { =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0throw new InvalidArgumentException(); =C2=A0=C2=A0=C2=A0 } =20 Neither of those are bad. The new syntax is just slightly more concise. =20 An implementation of the change can be found here: https://github.com/php/php-src/pull/5279 =20 It's also noteworthy that the same feature has semi-recently been added to = C# 7.0: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/= throw#the-throw-expression =20 This has been proposed a handful of times in the internals mailing list but= never extensively discussed: https://externals.io/message/49569 (2010) https://externals.io/message/15301 (2005) https://externals.io/message/10553 (2004) =20 Let me know what you think! =20 Also, to actually create the RFC I need RFC karma. Can someone grant me tho= se privileges? My username is ilijatovilo. =20 Thank you! --B_3667504211_762001550--