Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58411 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36359 invoked from network); 1 Mar 2012 13:55:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 13:55:51 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.49 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.49 mail-qw0-f49.google.com Received: from [209.85.216.49] ([209.85.216.49:37254] helo=mail-qw0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/76-46815-6EF7F4F4 for ; Thu, 01 Mar 2012 08:55:50 -0500 Received: by qafi29 with SMTP id i29so367766qaf.8 for ; Thu, 01 Mar 2012 05:55:47 -0800 (PST) Received-SPF: pass (google.com: domain of ircmaxell@gmail.com designates 10.224.168.79 as permitted sender) client-ip=10.224.168.79; Authentication-Results: mr.google.com; spf=pass (google.com: domain of ircmaxell@gmail.com designates 10.224.168.79 as permitted sender) smtp.mail=ircmaxell@gmail.com; dkim=pass header.i=ircmaxell@gmail.com Received: from mr.google.com ([10.224.168.79]) by 10.224.168.79 with SMTP id t15mr5273542qay.66.1330610147751 (num_hops = 1); Thu, 01 Mar 2012 05:55: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:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=6kkMNws5VtmcWGf4gS/Nbr/MFU2prdYDcXreHa8/RQk=; b=uceb90cITpUgGs28KJYv0HSmb+beCjIBRksnjE0KiejjMRd011iV1tu9/AO8GCB3dN eX98FQ5zz99Q0sPB4kiaPwnY6pbPaUjnl2uzk+1oek0eMmYAfiZbKNl8uZA2qJCBfSeq 4JHfqGcSQDR86p78KVdfBLXNjVdeoZJBpV9jc= MIME-Version: 1.0 Received: by 10.224.168.79 with SMTP id t15mr4395488qay.66.1330610147661; Thu, 01 Mar 2012 05:55:47 -0800 (PST) Received: by 10.229.233.71 with HTTP; Thu, 1 Mar 2012 05:55:47 -0800 (PST) In-Reply-To: References: Date: Thu, 1 Mar 2012 08:55:47 -0500 Message-ID: To: Lazare Inepologlou Cc: Adam Jon Richardson , internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Scalar Type Intentions From: ircmaxell@gmail.com (Anthony Ferrara) Please do not implement int, float, etc as an alias to scalar. That's going to cause nothing but trouble later on. It will instantly close the door to any type of casting magic (due to BC concerns), be completely non-obvious ("I hinted for int, why is it a boolean?"), and cause nothing but confusion. If you only want to add a scalar hint, I'm fine with that (for now), but please only add the scalar hint, no aliases... Anthony On Thu, Mar 1, 2012 at 8:33 AM, Lazare Inepologlou wro= te: > 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 case= s? > =A0Simple example: > > class A { > =A0public $value =3D 1234; > =A0public function __toString(){ return (string)$this->value; } > } > > function foo( int $x ) { =A0// here "int" is used as an alias to "scalar"= as > you suggest > =A0return $x + 1; > } > > $a =3D new A; > foo( $a ); =A0// casting $a to scalar upon calling, as it is possible aft= er > 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 th= at > I am not aware). > > > Lazare INEPOLOGLOU > Ing=E9nieur Logiciel > > > 2012/3/1 Adam Jon Richardson > >> On Thu, Mar 1, 2012 at 4:36 AM, Lazare Inepologlou w= rote: >> >>> 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 = for >>> "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{ >> =A0} >> >> function foo($arg1, $arg2, $arg3){ >> =A0 =A0if ($arg1) { >> =A0 =A0 =A0 return "The answer is: " . $arg1; >> =A0 =A0} >> >> =A0 =A0if ($arg2) { >> =A0 =A0 =A0 return "The answer is: " . $arg2; >> =A0 =A0} >> >> =A0 =A0if ($arg3) { >> =A0 =A0 =A0 return "The answer is: " . $arg3; >> =A0 =A0} >> } >> >> $test =3D new test(); >> >> echo foo($arg1 =3D "string", $arg2 =3D 100, $arg3 =3D $test); // echos T= he >> answer is: string >> echo foo($arg1 =3D false, $arg2 =3D 100, $arg3 =3D $test); // echos The = answer >> is: 100 >> echo foo($arg1 =3D false, $arg2 =3D false, $arg3 =3D $test); // catchabl= e 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 = 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 functio= n >> (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 i= n >> the foo() call is a small amount of noise/keystrokes), visibly conforman= t >> to the intentions of the foo() function, and more likely to catch bugs >> early on in the process. >> >> Adam >> >>