Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78169 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64635 invoked from network); 21 Oct 2014 01:17:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Oct 2014 01:17:28 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 198.187.29.244 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 198.187.29.244 imap1-3.ox.privateemail.com Received: from [198.187.29.244] ([198.187.29.244:47801] helo=imap1-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/04-42514-824B5445 for ; Mon, 20 Oct 2014 21:17:28 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 5F3DBB0008B; Mon, 20 Oct 2014 21:17:25 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap1.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap1.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id c8IMVnP-3xh6; Mon, 20 Oct 2014 21:17:25 -0400 (EDT) Received: from oa-res-26-28.wireless.abdn.ac.uk (oa-res-26-28.wireless.abdn.ac.uk [137.50.26.28]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 8E152B0007B; Mon, 20 Oct 2014 21:17:24 -0400 (EDT) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 8.0 \(1990.1\)) In-Reply-To: <9ADD6AAC-A402-44B6-B9AA-A3F5F8A225AD@fb.com> Date: Tue, 21 Oct 2014 02:17:21 +0100 Cc: PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <6C259842-9CA7-4058-9929-59CC3C9EA503@ajf.me> References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> <9ADD6AAC-A402-44B6-B9AA-A3F5F8A225AD@fb.com> To: Josh Watzman X-Mailer: Apple Mail (2.1990.1) Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: ajf@ajf.me (Andrea Faulds) > On 21 Oct 2014, at 02:07, Josh Watzman wrote: >=20 > Throwing an exception or even returning NULL seems so much better than = returning "false" -- "false" is a boolean, not an error, and despite = some historical cases of PHP using "false" as a poor person's error = code, it really isn=92t. Why isn=92t FALSE an error value? NULL signifies an absence of a value, = not a bad value. We use FALSE in a lot of places to indicate an error. = Heck, filter_var uses it for errors here. > If you want error codes, use error codes, or use exceptions, but = having two kinds of failure, null vs false, is really confusing. What two kinds of error? We=92re only using FALSE here. >> =95 If strict type hinting were added, they would fail for a = nullable typehint >=20 > 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 the return type of, for example, to_int is now "int | bool" = as opposed to "nullable-int" or "int" (if you throw). Exceptions make chaining more difficult. There=92s also no precedent for = using them. Returning NULL would mean it=92s validate for a nullable type hint, = which is bad, but also, NULL signifies a lack of a value. There isn=92t = a lack of a value, there=92s no value. We should not use NULL here. >> =95 FALSE is a traditional error value in PHP >=20 > Since this is a new function, one that doesn't interoperate in any = complicated way with the existing library or affect BC, this doesn't = seem that important. IMO a language should have one = failure/absense-of-value, in most cases "null", and having a weird = second case seems, well, weird. Failure and absence of value are different things and should not use the = same value. Otherwise, you are liable to confuse missing data and = errors. > If you have more interesting failure cases, just throw an exception, = or return null if you want, don't continue propagating a weird second = kind of null (that isn't actually null, it's a boolean false). It=92s not a =93weird second kind of null=94, it=92s a value FALSE. > Also, that we *couldn't* introduce a meaningful to_bool function, even = if we decided we wanted it, indicates to me that returning false is the = wrong thing to do. It would be possible to use a special value in that specific case. = Though, again, booleans have their own problems. I don=92t see the point = in to_bool(). > It's also interesting to look at how other languages handle failures = of this 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 of ways, throws. Functional languages do more interesting = things; Haskell does something complicated but morally equivalent to = returning null. But I'm not aware of any language which specifically = returns false. JavaScript returns NaN here and C sets errno to an error value. PHP uses = FALSE, in some respects, like JavaScript uses NaN. -- Andrea Faulds http://ajf.me/