Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58415 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 56624 invoked from network); 1 Mar 2012 15:36:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Mar 2012 15:36:43 -0000 Authentication-Results: pb1.pair.com header.from=linepogl@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=linepogl@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.161.170 as permitted sender) X-PHP-List-Original-Sender: linepogl@gmail.com X-Host-Fingerprint: 209.85.161.170 mail-gx0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:38459] helo=mail-gx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C9/6A-46815-A879F4F4 for ; Thu, 01 Mar 2012 10:36:43 -0500 Received: by ggki2 with SMTP id i2so307805ggk.29 for ; Thu, 01 Mar 2012 07:36:40 -0800 (PST) Received-SPF: pass (google.com: domain of linepogl@gmail.com designates 10.236.184.167 as permitted sender) client-ip=10.236.184.167; Authentication-Results: mr.google.com; spf=pass (google.com: domain of linepogl@gmail.com designates 10.236.184.167 as permitted sender) smtp.mail=linepogl@gmail.com; dkim=pass header.i=linepogl@gmail.com Received: from mr.google.com ([10.236.184.167]) by 10.236.184.167 with SMTP id s27mr8038300yhm.8.1330616200250 (num_hops = 1); Thu, 01 Mar 2012 07:36:40 -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=FfPcN5gDY3tK4+XUVjtApTHToBqVKdiU5euopNbEOFE=; b=bkKuuS+2LY4bgcE64cjzfqeVcTaGOhrfKV1cuGAZvItfDlTyOxlguxBj7iQbc7ybtR yla/v46YXvklr0bwHBfxRoFWYxQWq8REppQVmGJaOxhm3KYoO94AnjypnX145YdUwn4y hsnPP6fQO0Xrw+gmQzrsM2gVCrX806sVj4Lbc= Received: by 10.236.184.167 with SMTP id s27mr6299497yhm.8.1330616200183; Thu, 01 Mar 2012 07:36:40 -0800 (PST) MIME-Version: 1.0 Received: by 10.147.125.8 with HTTP; Thu, 1 Mar 2012 07:36:20 -0800 (PST) In-Reply-To: References: Date: Thu, 1 Mar 2012 16:36:20 +0100 Message-ID: To: Adam Jon Richardson Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf303f6accf1c12104ba30399d Subject: Re: [PHP-DEV] Scalar Type Intentions From: linepogl@gmail.com (Lazare Inepologlou) --20cf303f6accf1c12104ba30399d Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable > > Of note, the scalar type hinting I've outlined does not automatically > perform casts... Thank you for your answer. Maybe, this exact fact is what I don't like about your suggestion. Please read the following RFC, where Lukas Smith and Zeev Suraski explain very well why strict type checking without auto-casting is a not a great idea. Of course it is just an RFC, but I find it quite correct. https://wiki.php.net/rfc/typecheckingstrictandweak My concern is that if your suggestion is adopted (as it is, without auto-casting) then an eventual introduction of auto-casting will be impossible without breaking BC. Lazare INEPOLOGLOU Ing=C3=A9nieur Logiciel 2012/3/1 Adam Jon Richardson > On Thu, Mar 1, 2012 at 8:33 AM, Lazare Inepologlou wr= ote: > >> 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 cas= es? >> Simple example: >> > > I'm not sure I understand, so if I mischaracterize your concerns, please > let me know. > > Of note, the scalar type hinting I've outlined does not automatically > perform casts to any particular type of scalar. Rather, it would be the > programmer's responsibility to perform the cast (as I performed in my > example.) This way, only necessary, reasonable casts are performed, and > information loss can be avoided. > > That said, in terms of performance, PHP's type juggling performs these > types of casts all the time, so I don't think I'd be concerned. Any time = we > check for equality using =3D=3D, perform string concatenation with ints, = etc., > PHP's beautiful type juggling automatically performs these conversions fo= r > us without any effort on our part. I've never seen where this is the sour= ce > of any performance issues in my profiling, but I must admit that I don't > know the internals well enough to preclude this from ever being an issue. > > 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 hood= s >> that I am not aware). >> > > Speaking to your example, it would throw a catchable fatal error because > the variable $a contains an object of type A and the function foo expects= a > scalar. The object would first have to be cast to a scalar. However, as y= ou > pointed out, currently objects can only implement the __toString() method > (i.e., there's no __toInt, etc.), so one can't directly cast an object to > an int. > > This seems contrived, though, because in the case of your example, if a > function expects an integer, wouldn't you just call it with the appropria= te > object property: > > foo ($a->value); // works because the value property is a scalar (int) > > Thanks for your commentary :) > > Adam > > --20cf303f6accf1c12104ba30399d--