Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78164 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 54760 invoked from network); 20 Oct 2014 23:39:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Oct 2014 23:39:32 -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 192.64.116.216 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.216 imap10-3.ox.privateemail.com Received: from [192.64.116.216] ([192.64.116.216:34780] helo=imap10-3.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A5/12-42514-23D95445 for ; Mon, 20 Oct 2014 19:39:31 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 58D4B2400C3; Mon, 20 Oct 2014 19:39:28 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap10.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap10.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id Hof3kUaQZK-3; Mon, 20 Oct 2014 19:39:28 -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 92DB024005D; Mon, 20 Oct 2014 19:39:27 -0400 (EDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.0 \(1990.1\)) In-Reply-To: <9BB32AF2-E776-46CA-8604-0B5283B5050C@strojny.net> Date: Tue, 21 Oct 2014 00:39:25 +0100 Cc: PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> <9BB32AF2-E776-46CA-8604-0B5283B5050C@strojny.net> To: Lars Strojny 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 00:27, Lars Strojny wrote: >=20 > I like the proposal except for one thing: the functions returning = false in case of an error. As the next logical function would be = "to_bool()", I foresee a lot of trouble with regards to API design as = returning false there either means successful cast to false or an error = while casting. What about changing the functions to take an $isError = variable as a second argument? >=20 > $value =3D to_string(1.2, $isError); > if ($isError) { > ... > } >=20 > Alternatively one could do the same thing with an $isSuccess variable: >=20 > $value =3D to_string(1.2, $isSuccess); > if (!$isSuccess) { > ... > } >=20 > Thoughts? This is an interesting question. It=E2=80=99s actually one I=E2=80=99d = considered myself, I think I briefly mention it in the RFC. However, I = don=E2=80=99t expect it to be a problem, and the reason is really quite = simple: There=E2=80=99s no clear-cut one-size-fits all boolean casting = function you could make, and booleans are trivial to validate yourself. Adding something like is_bool() would be possible, the problem is what = behaviour it would have, and there are too many possibilities. Do you = accept 1 and 0? If so, do you accept all non-zero values, or not? Is = =E2=80=9Ctrue=E2=80=9D TRUE or is it FALSE? Is =E2=80=9Cyes=E2=80=9D = TRUE or is it FALSE? Do =E2=80=9Ctrue=E2=80=9D and =E2=80=9Cyes=E2=80=9D = validate? Do they not? Does the empty string validate? Does it not? Bear = in mind that boolean casting was one of the most problematic areas of = the abandoned Scalar Type Hinting RFC: there=E2=80=99s no clear set of = definitely boolean values, nor a clear idea of which mean which boolean = value. Basically, I don=E2=80=99t think we=E2=80=99d ever be able to = draw the line. The other thing is, well, there=E2=80=99s not really a need. You can = quite simply do something like this: if ($value !=3D=3D =E2=80=9Ctrue=E2=80= =9D && $value =3D=3D=3D =E2=80=9Cfalse=E2=80=9D) { /* error! */ } - and = while this is less convenient than is_bool, as I mentioned before, = there=E2=80=99s no real agreement on what is_bool should do so you=E2=80=99= d have to do this anyway. A thought: A weird, but workable solution for = one particular style is [=E2=80=9Ctrue=E2=80=9D =3D> TRUE, =E2=80=9Cfalse=E2= =80=9D =3D> FALSE][$value] ?? NULL - whether that=E2=80=99s a wonderful = or horrible use of PHP is up to you. ;) So I don=E2=80=99t think using FALSE here is a problem, since I doubt = is_bool would ever be added. Regarding using a reference variable for the output, I don=E2=80=99t = like that idea much. It doesn=E2=80=99t chain very well=E2=80=A6 and = would it fail a strict type hint, if we added that? Even if it somehow = did, I don=E2=80=99t think it is a good idea. Since I don=E2=80=99t = think is_bool will happen, there=E2=80=99s also no real need, either. -- Andrea Faulds http://ajf.me/