Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58410 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34372 invoked from network); 1 Mar 2012 13:33:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 13:33:50 -0000 Authentication-Results: pb1.pair.com smtp.mail=linepogl@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=linepogl@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.170 as permitted sender) X-PHP-List-Original-Sender: linepogl@gmail.com X-Host-Fingerprint: 209.85.213.170 mail-yx0-f170.google.com Received: from [209.85.213.170] ([209.85.213.170:39858] helo=mail-yx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A4/06-46815-DBA7F4F4 for ; Thu, 01 Mar 2012 08:33:49 -0500 Received: by yenl5 with SMTP id l5so205230yen.29 for ; Thu, 01 Mar 2012 05:33:47 -0800 (PST) Received-SPF: pass (google.com: domain of linepogl@gmail.com designates 10.236.177.6 as permitted sender) client-ip=10.236.177.6; Authentication-Results: mr.google.com; spf=pass (google.com: domain of linepogl@gmail.com designates 10.236.177.6 as permitted sender) smtp.mail=linepogl@gmail.com; dkim=pass header.i=linepogl@gmail.com Received: from mr.google.com ([10.236.177.6]) by 10.236.177.6 with SMTP id c6mr7032575yhm.42.1330608827234 (num_hops = 1); Thu, 01 Mar 2012 05:33:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=13VYLfQuKhsctyX83gqMsHzLuKty7JflWJmsCGo0Jl8=; b=e9TkcLkSJe2EtKUYzILJ7mUa7yKxkqo9gO1Jl6HFAez5z2Yopx4QYNg3EgOaRCTaXz axZD6dlmXDTKrlLQjD8MLY8kGBsHE1A2Qm765ESjAMNBJ3m5nBxfAr016ihMV63jeCpN SELc5bSIpbuWcFX92yo77jBbMR+FgKq/YpBf0= Received: by 10.236.177.6 with SMTP id c6mr5474214yhm.42.1330608827162; Thu, 01 Mar 2012 05:33:47 -0800 (PST) MIME-Version: 1.0 Received: by 10.147.125.8 with HTTP; Thu, 1 Mar 2012 05:33:27 -0800 (PST) In-Reply-To: References: Date: Thu, 1 Mar 2012 14:33:27 +0100 Message-ID: To: Adam Jon Richardson Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf305639017a5f4e04ba2e824b Subject: Re: [PHP-DEV] Scalar Type Intentions From: linepogl@gmail.com (Lazare Inepologlou) --20cf305639017a5f4e04ba2e824b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Yes, I agree, the casting (or the failing to cast) has to happen on entry, for the reasons that you have very well explained. However, I cannot understand what it means to cast an object to a scalar. Does it always mean casting to string? Wouldn't that be slow in many cases? Simple example: class A { public $value =3D 1234; public function __toString(){ return (string)$this->value; } } function foo( int $x ) { // here "int" is used as an alias to "scalar" as you suggest return $x + 1; } $a =3D new A; foo( $a ); // casting $a to scalar upon calling, as it is possible after all In this example, the integer value will have to be cast to a string only to be cast back to integer (unless something else happens under the hoods that I am not aware). Lazare INEPOLOGLOU Ing=C3=A9nieur Logiciel 2012/3/1 Adam Jon Richardson > On Thu, Mar 1, 2012 at 4:36 AM, Lazare Inepologlou wr= ote: > >> 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 t= o >> 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 f= or >> "scalar", then there will be no way to choose which type casting method >> should be chosen. >> >> >> Lazare INEPOLOGLOU >> Ing=C3=A9nieur 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 Th= e > answer is: string > echo foo($arg1 =3D false, $arg2 =3D 100, $arg3 =3D $test); // echos The a= nswer > is: 100 > echo foo($arg1 =3D false, $arg2 =3D false, $arg3 =3D $test); // catchable= fatal > 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 t= he > 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 > > --20cf305639017a5f4e04ba2e824b--