Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78228 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 39361 invoked from network); 22 Oct 2014 12:35:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2014 12:35:58 -0000 Authentication-Results: pb1.pair.com smtp.mail=bobwei9@hotmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=bobwei9@hotmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain hotmail.com designates 65.55.111.153 as permitted sender) X-PHP-List-Original-Sender: bobwei9@hotmail.com X-Host-Fingerprint: 65.55.111.153 blu004-omc4s14.hotmail.com Received: from [65.55.111.153] ([65.55.111.153:50154] helo=BLU004-OMC4S14.hotmail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 10/55-01590-CA4A7445 for ; Wed, 22 Oct 2014 08:35:56 -0400 Received: from BLU437-SMTP48 ([65.55.111.137]) by BLU004-OMC4S14.hotmail.com over TLS secured channel with Microsoft SMTPSVC(7.5.7601.22751); Wed, 22 Oct 2014 05:35:53 -0700 X-TMN: [36ZfTYcW9X1E2AFaJNx1Ym4KhWgIuz3A] X-Originating-Email: [bobwei9@hotmail.com] Message-ID: Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 (Mac OS X Mail 8.0 \(1990.1\)) In-Reply-To: Date: Wed, 22 Oct 2014 14:35:45 +0200 CC: Andrea Faulds , PHP Internals Content-Transfer-Encoding: quoted-printable References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> To: Dmitry Stogov X-Mailer: Apple Mail (2.1990.1) X-OriginalArrivalTime: 22 Oct 2014 12:35:51.0260 (UTC) FILETIME=[B6DA59C0:01CFEDF4] Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: bobwei9@hotmail.com (Weinand Bob) So, what exactly changes here if we have a second parameter or just = return null by default? It doesn=E2=80=99t make any difference, it=E2=80=99s just another way to = write it: to_int($a, $default) or to_int($a) ?? $default Also, if you want exceptions, you always can wrap a userland function = around it =E2=80=94 but I=E2=80=99d rather not wrap an userland function = around something throwing an exception=E2=80=A6 inefficient and weird. Thanks, Bob > Am 22.10.2014 um 12:27 schrieb Dmitry Stogov : >=20 > "null" or "false" return value would make these functions not really > useful, because they won't guarantee to return desired type. >=20 > printf("%d\n", to_int("abcd")); // will print 0 >=20 > The only reliable option to support wrong input is exceptions. > On the other hand, exceptions maybe difficult to use or inefficient. > We may avoid exceptions throwing, if provide a default value: >=20 > function to_int(mixed $a , int $default_value =3D null): int; > function to_double(mixed $a , double $default_value =3D null): double; > function to_string(mixed $a, string $default_value =3D null): string; >=20 > Thanks. Dmitry. >=20 > On Wed, Oct 22, 2014 at 12:37 PM, Bob Weinand = wrote: >=20 >> I know we have that already discussed a lot now, but I=E2=80=99d like = to expose my >> points on the return value here: >>=20 >> I imagine code like (supposing that we ever will have scalar = typehints): >>=20 >> function acceptsInt (int $i =3D null) { >> if ($i =3D=3D=3D null) { >> $i =3D 2 /* default value */; >> } >> /* do something with $i */ >> } >>=20 >> When we return false: >> acceptInt(($tmp =3D to_int($_GET["userinput"])) =3D=3D=3D false ? = null : $tmp); >>=20 >> When we throw an exception: >> try { >> acceptInt(to_int($_GET["userinput"])); >> } catch (CastingException $e) { >> acceptInt(null); >> } >>=20 >> When we just return null: >> acceptInt(to_int($_GET["userinput"])); >>=20 >> Also, when we want to pass a default value defined outside of the >> function, it=E2=80=99s a lot easier now with the coalesce operator: >> acceptInt(to_int($_GET["userinput=E2=80=9C]) ?? 2 /* default value = */); >>=20 >>=20 >> Also, independently of possible scalar typehints: >>=20 >> Generally exceptions are also a bad idea as the casts probably will = be >> used on external input and exceptions are **not** a way to handle = malformed >> user input. Really not. >> Furthermore, false is a bad idea in the same sense (if we get scalar = type >> hints once), because people then might just catch the = EngineException=E2=80=A6 >>=20 >> Also, null means "no value"; that=E2=80=99s exactly what we need. If = the >> to_{type}() functions cannot return a meaningful value, just return = "no >> value", that means null. And not false, which is a real value. >>=20 >> That=E2=80=99s why I strongly feel that null is the only true thing = to return here. >>=20 >> Thanks, >> Bob >>=20 >>> Am 21.10.2014 um 00:57 schrieb Andrea Faulds : >>>=20 >>> 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! >>> -- >>> Andrea Faulds >>> http://ajf.me/