Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78167 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61553 invoked from network); 21 Oct 2014 01:07:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Oct 2014 01:07:20 -0000 Authentication-Results: pb1.pair.com header.from=jwatzman@fb.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=jwatzman@fb.com; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain fb.com does not designate 67.231.145.42 as permitted sender) X-PHP-List-Original-Sender: jwatzman@fb.com X-Host-Fingerprint: 67.231.145.42 mx0a-00082601.pphosted.com Linux 2.5 (sometimes 2.4) (4) Received: from [67.231.145.42] ([67.231.145.42:30023] helo=mx0a-00082601.pphosted.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A3/53-42514-6C1B5445 for ; Mon, 20 Oct 2014 21:07:19 -0400 Received: from pps.filterd (m0004347 [127.0.0.1]) by m0004347.ppops.net (8.14.5/8.14.5) with SMTP id s9L16pvO005275 for ; Mon, 20 Oct 2014 18:07:16 -0700 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=NBEJeostgaI9DCTKhshJ/DUFnMih06NCdr0lwAZbu48=; b=kEXSCcPMGkzyfkn+ZwDt72wloiTHW+ez+qg7eDD51j4CHfMv1T7Y+Osc8LLdtlHEd6fH 9SxM5hjYevogucuj5vbzxrThTuTGiSSiCaigbxy2eD1qH809yJhsl0HsmigfNkA9oTdX CuXJtRfSjzM5yhUKl4YLWose5RsaS8FNGfk= Received: from pps.reinject (localhost [127.0.0.1]) by m0004347.ppops.net with ESMTP id 1q58cv8gmr-1 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO) for ; Mon, 20 Oct 2014 18:07:15 -0700 Received: from m0004347 (m0004347 [127.0.0.1]) by pps.reinject (8.14.5/8.14.5) with SMTP id s9L17F4Z005954 for ; Mon, 20 Oct 2014 18:07:15 -0700 Received: from mail.thefacebook.com (mailwest.thefacebook.com [173.252.71.148]) by m0004347.ppops.net with ESMTP id 1q58cv8gmq-1 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=OK); Mon, 20 Oct 2014 18:07:15 -0700 Received: from PRN-MBX02-2.TheFacebook.com ([169.254.5.69]) by PRN-CHUB15.TheFacebook.com ([fe80::3479:9544:c2df:1fc0%12]) with mapi id 14.03.0195.001; Mon, 20 Oct 2014 18:07:14 -0700 To: Andrea Faulds CC: PHP Internals Thread-Topic: [PHP-DEV] [RFC] Safe Casting Functions Thread-Index: AQHP7LlZiSKK4iJP+UuYlFeFZanGU5w6MpeA Date: Tue, 21 Oct 2014 01:07:13 +0000 Message-ID: <9ADD6AAC-A402-44B6-B9AA-A3F5F8A225AD@fb.com> References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> In-Reply-To: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> 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="Windows-1252" Content-ID: <502CCF555D326440BF6689A921238CFB@fb.com> Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.12.52,1.0.28,0.0.0000 definitions=2014-10-20_05:2014-10-20,2014-10-20,1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 kscore.is_bulkscore=4.60055327167197e-11 kscore.compositescore=0 circleOfTrustscore=71.128 compositescore=0.999749537164123 urlsuspect_oldscore=0.999749537164123 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=8891 rbsscore=0.999749537164123 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-1410210011 X-FB-Internal: deliver Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: jwatzman@fb.com (Josh Watzman) On Oct 20, 2014, at 3:57 PM, Andrea Faulds wrote: > Good evening, >=20 > I am presenting a new RFC to add a set of three functions to do validated= casts for scalar types: >=20 > https://wiki.php.net/rfc/safe_cast >=20 > Please read it. >=20 > Thanks! I think this is pretty cool, but I'm really worried about some of its typin= g implications. In particular: > The functions return FALSE on failure instead of NULL because: Throwing an exception or even returning NULL seems so much better than retu= rning "false" -- "false" is a boolean, not an error, and despite some histo= rical cases of PHP using "false" as a poor person's error code, it really i= sn't. If you want error codes, use error codes, or use exceptions, but havi= ng two kinds of failure, null vs false, is really confusing. Addressing your arguments one by one: > =95 If strict type hinting were added, they would fail for a nullable ty= pehint Throwing an exception also addresses this, in a much cleaner way. If you're= worried about strict typing, then returning "false" is even worse, since t= he return type of, for example, to_int is now "int | bool" as opposed to "n= ullable-int" or "int" (if you throw). > =95 FALSE is a traditional error value in PHP Since this is a new function, one that doesn't interoperate in any complica= ted way with the existing library or affect BC, this doesn't seem that impo= rtant. IMO a language should have one failure/absense-of-value, in most cas= es "null", and having a weird second case seems, well, weird. If you have m= ore interesting failure cases, just throw an exception, or return null if y= ou want, don't continue propagating a weird second kind of null (that isn't= actually null, it's a boolean false). Also, that we *couldn't* introduce a meaningful to_bool function, even if w= e decided we wanted it, indicates to me that returning false is the wrong t= hing to do. > =95 NULL is used to signify the absence of a value - but what we want to= signify is an invalid value (similar to 0 vs NaN) I might argue that failure is indeed absense-of-value, but an exception see= ms like a much better way to convey this. It's also interesting to look at how other languages handle failures of thi= s kind. Most imperative languages I know of either throw or return null; in= particular, Python, which has fairly similar type system to PHP in a lot o= f ways, throws. Functional languages do more interesting things; Haskell do= es something complicated but morally equivalent to returning null. But I'm = not aware of any language which specifically returns false.=