Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109208 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27619 invoked from network); 22 Mar 2020 20:15:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Mar 2020 20:15:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9FA301804E0 for ; Sun, 22 Mar 2020 11:39:43 -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=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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-qufo17281301.me.com (ms11p00im-qufo17281301.me.com [17.58.38.50]) (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 ; Sun, 22 Mar 2020 11:39:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=me.com; s=1a1hai; t=1584902380; bh=HSezBfM2k8f7HdkzIpEpndeVCZOzAhc/mhGWHEyDfUY=; h=Date:Subject:From:To:Message-ID:Content-type; b=SkYu9iwCAJbOv9sat2SAs57P5yH3ylJjjhc0E/AQvG8L1zltOhx83iYrDs287ewPa rqRGBFwbxMO6pkne4is7qzu2O2sQVq06zRVc6oiY00WYjUDLR/ATQbuS3NyFKUVvX2 wxNPc3zkzONFmcZumunDI1WgY8NjmzYcqCiJJW+jgOWM3dInSyFvrDBq8StD43F56x sqmD6iq1rVIDH41BYsEEVO125kUh2shoNXHvJ/ehN6ibHKEJxaDy7Q7DTrCeiCNxjQ VaSOXiaBadEGX1+UgSHulszfwJg81+VZFYkdRlaPq8jNx2N08aivm+9/e3LjcQlRJI KjcRWapfa+D3w== Received: from [192.168.1.106] (9.66.194.178.dynamic.wline.res.cust.swisscom.ch [178.194.66.9]) by ms11p00im-qufo17281301.me.com (Postfix) with ESMTPSA id 62F9DAC02AE; Sun, 22 Mar 2020 18:39:39 +0000 (UTC) User-Agent: Microsoft-MacOutlook/16.35.20030802 Date: Sun, 22 Mar 2020 19:39:34 +0100 To: Mike Schinkel , Dan Ackroyd CC: PHP internals Message-ID: <77F0B565-E360-435A-9F56-03CE0545B48E@me.com> Thread-Topic: [PHP-DEV] [RFC][DISCUSSION] throw expression References: <5BEF8264-4003-4056-A31C-FF8BEE85FED6@me.com> In-Reply-To: Mime-version: 1.0 Content-type: text/plain; charset="UTF-8" Content-transfer-encoding: quoted-printable X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2020-03-22_07:,, 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=907 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1908290000 definitions=main-2003220112 Subject: Re: [PHP-DEV] [RFC][DISCUSSION] throw expression From: ilija.tovilo@me.com (Ilija Tovilo) Hi Mike Thanks for your feedback! Your solution works well and it's true that PHP would do just fine without = accepting this RFC. However I do think this RFC makes sense for a few reasons: * I think that most people would expect some of the examples (especially th= e arrow function) to be valid PHP code * Adding a throwException function will unnecessarily fragment your codebas= e into throw statements and calls to that function * It's not as static analysis friendly * The patch has very low complexity I agree that some languages do well with returning errors instead of throwi= ng them. I personally don't think it's a good fit for PHP because PHP can't = enforce you to handle these cases. Regards =EF=BB=BFOn 22.03.20, 19:14, "Mike Schinkel" wrote: > On Mar 22, 2020, at 1:16 PM, Dan Ackroyd wro= te: >=20 > On Sun, 22 Mar 2020 at 16:17, Ilija Tovilo wrot= e: >>=20 >> Due to the modest feedback I=E2=80=99d like to move the throw expression R= FC to =E2=80=9Cunder discussion=E2=80=9D. >>=20 >> https://wiki.php.net/rfc/throw_expression >>=20 >=20 > Regarding the example: >=20 > $condition || throw new Exception('$condition must be truthy') > && $condition2 || throw new Exception('$condition2 must be truthy'); >=20 > The "Deprecate left-associative ternary operator"* RFC made it so tha= t > parentheses are required when ternary operators are nested in > complicated statements. >=20 > Would a similar requirement for parentheses around complicated throw > expressions be a suitable solution to avoid people being surprised by > the behaviour? >=20 =20 Why can't you just do this in userland code? =20 function throwException(Exception $exception) { throw $exception; } =20 $callable =3D fn() =3D> throwException( new Exception() ); =20 // $value is non-nullable. $value =3D $nullableValue ?? throwException( new InvalidArgumentException= () );=20 =20 // $value is truthy. $value =3D $falsableValue ?: throwException( new InvalidArgumentException= () ); =20 // $value is only set if the array is not empty. $value =3D !empty($array) ? reset($array) : throwException( new InvalidArgumentException() ); =20 =20 -Mike P.S. I am probably in the vast minority on this list but I would like t= o see fewer places that throw exceptions, not more.=20 =20 I want to deal with errors where they happen, not throw exceptions or h= ave to catch them as I have found that I can write much more robust and easi= er to read code when I write without using exceptions. I came to this realiz= ation because of learning that Go does not endorse exceptions and then I lea= rned why they do not which strongly resonated with me. After that, I finally= felt comfortable saying that "Exceptions seemed like a good idea at the tim= e." =20 I now have a whole slew of classes who only purpose is to wrap PHP func= tions and classes that throw exceptions so I can call them w/o having to use= try{}catch{}. Instead I use an if() afterwards to check and then handle it = if there was an error. =20 One particularizing problematic exception in PHP is with the DataTime c= lass. I have found it effectively impossible to create a class that extends = DateTime without having the potential for an exception to be thrown (unless = someone knows a way that I do not?) The potential is actually hypothetical,= but PhpStorm nonetheless still complains that I have not handled exceptions= when using that child class.