Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78229 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41319 invoked from network); 22 Oct 2014 12:49:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Oct 2014 12:49:12 -0000 Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.214.181 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.214.181 mail-ob0-f181.google.com Received: from [209.85.214.181] ([209.85.214.181:51842] helo=mail-ob0-f181.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9C/A5-01590-7C7A7445 for ; Wed, 22 Oct 2014 08:49:11 -0400 Received: by mail-ob0-f181.google.com with SMTP id wm4so2756862obc.40 for ; Wed, 22 Oct 2014 05:49:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=kwaG5foQzjeSWo2aTR81SOR7HYVg4NAJ35YBxcPYuO0=; b=aqDZkRXM4XWq4LPF8KiK0pkC9aS47bzpYQ/ZbLsFlgkWDCmHSdezZC4BOczbsou9zy OSCOn9DY+xiwgq+eJzPgKjDqlSOJFZTFUW6KLGPV7A7TXoPoGg9IP22Ga2iRWZWG3JKp V+KKpBy95esZU/VJmDXB+G0/XmiJQP54taHtAXdj9/1LziR0lhdlI1xHLQU7FBvKjXsf tP7rXk3cCJ7+rAmt2rNReDsyRTh+qE3C979bt4x89FvgHQXSxW6++76aAeuNsiZUlTZO PJL3zjVwFK3JqYjiMhb5c7RMq3eBneHj8VSQjxAbBwYd9Zm++uflbwYVXq+SeioKncl/ imLQ== X-Gm-Message-State: ALoCoQlxW6jOl5oTulRQzpc2RSmtPRgDQuUQjRVFyWOT3KQ35VRYgV7KxKiWdw1QMU0seUwNN/qUiKHmWBPJ0p6PrbIw++BnMCVsTBuvNxOLLZwdkASKbcGLOYWnkG3q9LaSBzECdHNnz1y7asXdRMB8hPQbge9u+A== MIME-Version: 1.0 X-Received: by 10.182.33.138 with SMTP id r10mr930221obi.67.1413982147752; Wed, 22 Oct 2014 05:49:07 -0700 (PDT) Received: by 10.60.70.41 with HTTP; Wed, 22 Oct 2014 05:49:07 -0700 (PDT) In-Reply-To: References: <66B7B28C-2651-4A71-AC2A-55D4C7BB3DDC@ajf.me> Date: Wed, 22 Oct 2014 16:49:07 +0400 Message-ID: To: Weinand Bob Cc: Andrea Faulds , PHP Internals Content-Type: multipart/alternative; boundary=001a11c2cb9aa2ee8e0506025f20 Subject: Re: [PHP-DEV] [RFC] Safe Casting Functions From: dmitry@zend.com (Dmitry Stogov) --001a11c2cb9aa2ee8e0506025f20 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable for me it's weird that to_int() that must return "int" may return not "int"= . NULL with ?? seems better than FALSE :) but if we talk about safety, we should be able to relay on to_int() return value without additional checks. Thanks. Dmitry. On Wed, Oct 22, 2014 at 4:35 PM, Weinand Bob wrote: > So, what exactly changes here if we have a second parameter or just retur= n > 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 : > > > > "null" or "false" return value would make these functions not really > > useful, because they won't guarantee to return desired type. > > > > printf("%d\n", to_int("abcd")); // will print 0 > > > > 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: > > > > 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; > > > > Thanks. Dmitry. > > > > On Wed, Oct 22, 2014 at 12:37 PM, Bob Weinand > wrote: > > > >> 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: > >> > >> I imagine code like (supposing that we ever will have scalar typehints= ): > >> > >> function acceptsInt (int $i =3D null) { > >> if ($i =3D=3D=3D null) { > >> $i =3D 2 /* default value */; > >> } > >> /* do something with $i */ > >> } > >> > >> When we return false: > >> acceptInt(($tmp =3D to_int($_GET["userinput"])) =3D=3D=3D false ? null= : $tmp); > >> > >> When we throw an exception: > >> try { > >> acceptInt(to_int($_GET["userinput"])); > >> } catch (CastingException $e) { > >> acceptInt(null); > >> } > >> > >> When we just return null: > >> acceptInt(to_int($_GET["userinput"])); > >> > >> 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 */)= ; > >> > >> > >> Also, independently of possible scalar typehints: > >> > >> 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 > >> > >> Also, null means "no value"; that=E2=80=99s exactly what we need. If t= he > >> to_{type}() functions cannot return a meaningful value, just return "n= o > >> value", that means null. And not false, which is a real value. > >> > >> That=E2=80=99s why I strongly feel that null is the only true thing to= return > here. > >> > >> Thanks, > >> Bob > >> > >>> Am 21.10.2014 um 00:57 schrieb Andrea Faulds : > >>> > >>> Good evening, > >>> > >>> I am presenting a new RFC to add a set of three functions to do > >> validated casts for scalar types: > >>> > >>> https://wiki.php.net/rfc/safe_cast > >>> > >>> Please read it. > >>> > >>> Thanks! > >>> -- > >>> Andrea Faulds > >>> http://ajf.me/ > --001a11c2cb9aa2ee8e0506025f20--