Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79552 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 95987 invoked from network); 11 Dec 2014 00:34:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Dec 2014 00:34:02 -0000 Authentication-Results: pb1.pair.com smtp.mail=jwatzman@fb.com; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=jwatzman@fb.com; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain fb.com does not designate 67.231.153.30 as permitted sender) X-PHP-List-Original-Sender: jwatzman@fb.com X-Host-Fingerprint: 67.231.153.30 mx0b-00082601.pphosted.com Linux 2.5 (sometimes 2.4) (4) Received: from [67.231.153.30] ([67.231.153.30:4822] helo=mx0a-00082601.pphosted.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/31-19688-976E8845 for ; Wed, 10 Dec 2014 19:34:01 -0500 Received: from pps.filterd (m0004003 [127.0.0.1]) by mx0b-00082601.pphosted.com (8.14.5/8.14.5) with SMTP id sBB0Xwe8024489 for ; Wed, 10 Dec 2014 16:33:58 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fb.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-id : content-transfer-encoding : mime-version; s=facebook; bh=iukCwMVcLIDYQ7KUk051mwKKcNCZn92PXZVpTRJAfnk=; b=Gsks4LLR8MItTUPzeEgGtfzuDNoYszL3FVZRbkbNmqsdU9UFzvgVG1gy+cBwmSQ6ddr0 xLKMpv8UaGNURuhm4r423HN35rf7PheKrTaE6Thvl6hLnHXbDVAIkNA8GuxSpBhvQ9Cs ANDduGQsvcALr16bF9SOon6U81K2Q8eRl70= Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-00082601.pphosted.com with ESMTP id 1r6wya0paa-1 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Wed, 10 Dec 2014 16:33:58 -0800 Received: from m0004003 (m0004003 [127.0.0.1]) by pps.reinject (8.14.5/8.14.5) with SMTP id sBB0XwB9024484 for ; Wed, 10 Dec 2014 16:33:58 -0800 Received: from mail.thefacebook.com ([199.201.64.23]) by mx0b-00082601.pphosted.com with ESMTP id 1r6wya0pa1-3 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=OK); Wed, 10 Dec 2014 16:33:58 -0800 Received: from PRN-MBX02-2.TheFacebook.com ([169.254.5.125]) by PRN-CHUB08.TheFacebook.com ([fe80::c9c7:30fd:ad3:b94%12]) with mapi id 14.03.0195.001; Wed, 10 Dec 2014 16:33:50 -0800 To: Christoph Becker CC: Andrea Faulds , PHP internals Thread-Topic: [PHP-DEV] [RFC] Nullsafe calls Thread-Index: AQHQFATxvp9RjOsogUO8aMy5/EMi+5yJmRsAgAAfIACAAFRDgIAABdyA Date: Thu, 11 Dec 2014 00:33:50 +0000 Message-ID: <765E5B7F-6605-465F-81DC-BCB1C3D6E015@fb.com> References: <95A581EE-A062-4926-BE44-BCA87FC9B356@fb.com> <9230CB24-22FC-4A0E-A9D5-F02523B65A02@ajf.me> <583B9A4B-68FD-4FB1-B043-0D86E2480EAD@fb.com> <5488E187.9010801@gmx.de> In-Reply-To: <5488E187.9010801@gmx.de> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [192.168.16.4] Content-Type: text/plain; charset="us-ascii" Content-ID: <775EC716CDDD7344BF68F234CBF9A2C9@fb.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.13.68,1.0.33,0.0.0000 definitions=2014-12-10_09:2014-12-10,2014-12-10,1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 kscore.is_bulkscore=0 kscore.compositescore=0 circleOfTrustscore=15.112 compositescore=0.994924612563162 urlsuspect_oldscore=0.994924612563162 suspectscore=0 recipient_domain_to_sender_totalscore=0 phishscore=0 bulkscore=0 kscore.is_spamscore=0 recipient_to_sender_totalscore=0 recipient_domain_to_sender_domain_totalscore=1889 rbsscore=0.994924612563162 spamscore=0 recipient_to_sender_domain_totalscore=0 urlsuspectscore=0.9 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=7.0.1-1402240000 definitions=main-1412110004 X-FB-Internal: deliver Subject: Re: [PHP-DEV] [RFC] Nullsafe calls From: jwatzman@fb.com (Josh Watzman) On Dec 10, 2014, at 4:12 PM, Christoph Becker wrote: > Josh Watzman wrote: >=20 >> However, for a lot of failures, I don't feel that exceptions are >> appropriate. I tend to only use them for exceptional behavior -- >> usually, some failure that needs to be propagated up a few levels up >> the stack, to an appropriate error boundary. This doesn't necessarily >> mean a completely unrecoverable error, but it's *locally* >> unrecoverable. For things that are completely recoverable, returning >> null might be more appropriate and more lightweight, and then this >> nullsafe operator would be useful at that point. >=20 > FWIW, there is a third option: instead of returning null, return a null > object[1]. That causes (slightly) more overhead, but it's quite > flexible and works rather nicely without requiring the (IMO) ugly ?-> > nor exception handling. >=20 > Consider a non-existent user "foo": >=20 > User::find('foo')?->getName() // returns null >=20 > User::find('foo')->getName() // could also return '' or 'Unknown user' This is actually what the implementation does behind the scenes, but issues= with this approach actually being visible to userlevel code is what prompt= ed the feature in the first place :) The biggest problem is that you can ac= cidentally leak your null object, which causes the calling code to unexpect= edly swallow errors; with the ?-> operator, you know you're always immediat= ely using the null object, there's no way to hold onto it. Smaller problems= are that you either need a different null object for every object you want= to use this with, or a single general one with __call, which is ugly and h= ard to optimize. It's also impossible to typecheck, but that was a problem = for Hack and is pretty irrelevant to PHP. It's also worth noting that doing this with a null object would have the sa= me lack of short circuiting that I've been arguing for ;) Josh