Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58409 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31634 invoked from network); 1 Mar 2012 13:04:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 13:04:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=adamjonr@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=adamjonr@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: adamjonr@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-tul01m020-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:48355] helo=mail-tul01m020-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 71/85-46815-5D37F4F4 for ; Thu, 01 Mar 2012 08:04:22 -0500 Received: by obbwd1 with SMTP id wd1so713217obb.29 for ; Thu, 01 Mar 2012 05:04:19 -0800 (PST) Received-SPF: pass (google.com: domain of adamjonr@gmail.com designates 10.182.31.47 as permitted sender) client-ip=10.182.31.47; Authentication-Results: mr.google.com; spf=pass (google.com: domain of adamjonr@gmail.com designates 10.182.31.47 as permitted sender) smtp.mail=adamjonr@gmail.com; dkim=pass header.i=adamjonr@gmail.com Received: from mr.google.com ([10.182.31.47]) by 10.182.31.47 with SMTP id x15mr1820710obh.76.1330607059033 (num_hops = 1); Thu, 01 Mar 2012 05:04:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=X6gWma+BLci4I9Mw35Qtu/ahNXCHGJgPF/2CFIOfuNA=; b=PXliqXmEMDZ/2a+HGcSdP+5nOOpBvHu98QsiN68QjAygCp/kUPhrzQUhHMfVfvrq+x U916QWLwQ/Alnp3+E5A+qSyEpFv0BJPMq1bKNy79y5Za1iI6ef4Fc+H63pAblBfejbZX 6z9niskpuYpJCxwH3NRHeNpCk9U4UBhtJRLpY= MIME-Version: 1.0 Received: by 10.182.31.47 with SMTP id x15mr1589897obh.76.1330607058963; Thu, 01 Mar 2012 05:04:18 -0800 (PST) Received: by 10.182.19.104 with HTTP; Thu, 1 Mar 2012 05:04:18 -0800 (PST) In-Reply-To: References: Date: Thu, 1 Mar 2012 08:04:18 -0500 Message-ID: To: Lazare Inepologlou Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=14dae93b590e15cb6b04ba2e196c Subject: Re: [PHP-DEV] Scalar Type Intentions From: adamjonr@gmail.com (Adam Jon Richardson) --14dae93b590e15cb6b04ba2e196c Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, Mar 1, 2012 at 4:36 AM, Lazare Inepologlou wrot= e: > And, *what if PHP added the following aliases for the hint scalar*: > > - bool > > - int > > - float > > - string >> > > If an object has a __toString method, does it qualify as a valid value to > be passed to a scalar argument? In my opinion, it should. > > Your suggestion has a future compatibility problem. The introduction of > new type casting methods (like __toInt or like __castTo) is an open > possibility. In such a case, if those keywords are nothing but aliases fo= r > "scalar", then there will be no way to choose which type casting method > should be chosen. > > > Lazare INEPOLOGLOU > Ing=E9nieur Logiciel > You raise interesting points, Lazare, but I don't believe the compatibility issues you're concerned about are valid. Failing fast, similar to the Poka-Yoke principal in manufacturing, suggests that system failure should occur as soon as possible to reduce software bugs: http://www.martinfowler.com/ieeeSoftware/failFast.pdf Consider the following example: // example class that does not implement __toString() class test{ } function foo($arg1, $arg2, $arg3){ if ($arg1) { return "The answer is: " . $arg1; } if ($arg2) { return "The answer is: " . $arg2; } if ($arg3) { return "The answer is: " . $arg3; } } $test =3D new test(); echo foo($arg1 =3D "string", $arg2 =3D 100, $arg3 =3D $test); // echos The = answer is: string echo foo($arg1 =3D false, $arg2 =3D 100, $arg3 =3D $test); // echos The ans= wer is: 100 echo foo($arg1 =3D false, $arg2 =3D false, $arg3 =3D $test); // catchable f= atal error A developer using this function would only see this issue some of the time, as this code fails late WITHIN some branches of the function, and the bug is harder to identify. We can do better, though. If the scalar type hint were applied, users would merely have to cast the object to a string ON ENTRY to the function (and, of note, this would work for your __toInt and __castTo concerns): echo foo($arg1 =3D "string", $arg2 =3D 100, $arg3 =3D (string)$test); // catchable fatal error Because the cast is performed on entry to the call, the bug shows up immediately. I would argue that this code is clean (the cast to string in the foo() call is a small amount of noise/keystrokes), visibly conformant to the intentions of the foo() function, and more likely to catch bugs early on in the process. Adam --14dae93b590e15cb6b04ba2e196c--