Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75536 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 56413 invoked from network); 15 Jul 2014 12:19:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2014 12:19:51 -0000 Authentication-Results: pb1.pair.com smtp.mail=tyra3l@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=tyra3l@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.50 as permitted sender) X-PHP-List-Original-Sender: tyra3l@gmail.com X-Host-Fingerprint: 209.85.192.50 mail-qg0-f50.google.com Received: from [209.85.192.50] ([209.85.192.50:63105] helo=mail-qg0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5B/7B-15121-56C15C35 for ; Tue, 15 Jul 2014 08:19:49 -0400 Received: by mail-qg0-f50.google.com with SMTP id q108so600429qgd.23 for ; Tue, 15 Jul 2014 05:19:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Gt0VizmjCtuLKpvT1garaKqQmViYXrw68ONhOvz1o2I=; b=QeoKzSw5fynzcSpy05L6hdl6pHn1gaIkKZp0074WoC2R9ppvhjh5+rIXST6E+iAJ3Z BYE3wrn9cpTiEspabG7kuzMtjLlxaWQIajtkAj14XlW7Ksj4sJwXYQF6ZKkPSzvZSuek McwHdKRTCI2Q3ZPi115NzL2r4OLojTOrFewdNT9rQw9aSe/7hxORx0AlNLjqGGeoa2dK SwjnfZq2YfwU+fNPmxAVLCBoxRw9oSp5sb6zaQyB4GOBhue3Fq7IaijVhevmVtcJRTDV D14Xg+nHXIfmjaAYCXjicv46DUUA4vlAJPbsCIDWAUWgpFgdO2sbEgqzavTglZ6Ehbut PvCw== MIME-Version: 1.0 X-Received: by 10.229.193.66 with SMTP id dt2mr27215822qcb.1.1405426786541; Tue, 15 Jul 2014 05:19:46 -0700 (PDT) Received: by 10.140.102.111 with HTTP; Tue, 15 Jul 2014 05:19:46 -0700 (PDT) In-Reply-To: References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <53C3F70A.7010706@gmail.com> <3aa29e080d70c843713e19a952dfbad6@mail.gmail.com> <20140714170149.GE27743@phcomp.co.uk> <20140714171446.GG27743@phcomp.co.uk> Date: Tue, 15 Jul 2014 14:19:46 +0200 Message-ID: To: Kristopher Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a11c27caa5ea84e04fe3a6cff Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: tyra3l@gmail.com (Ferenc Kovacs) --001a11c27caa5ea84e04fe3a6cff Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Mon, Jul 14, 2014 at 7:19 PM, Kristopher wrote: > On Mon, Jul 14, 2014 at 1:14 PM, Alain Williams wrote= : > > > On Mon, Jul 14, 2014 at 06:09:22PM +0100, Andrea Faulds wrote: > > > > > > On 14 Jul 2014, at 18:01, Alain Williams wrote: > > > > > > > But if you have: > > > > > > > > function foo(int $a) { > > > > ... > > > > $a =3D 3 / 2; > > > > } > > > > > > > > What do you expect $a to contain ? I would suggest integer 1. > > > > > > I would have expected 1 - since it appears, from the code, that $a should > > only contain integers. > > > What about the current type hinting we have, then? > > function foo(Bar $a) { > $a =3D 3 / 2; > } > > Perfectly OK. Why would we treat scalars any different? > this was asked and answered a dozen of times in the past, but let me repeat once again: php does support type juggling/coercion for scalar types, but not for complex types. introducing "strict" typehints for complex types was safe, as there are no expectation from the users, if you expect an instance of Foo and you got a Bar (which isn't a subclass of Foo) then you know that you screwed up. (personally I think that the adding the support for array to the strict typehints was a bad idea, but that is out of scope here.) But people do have expectations about the scalar types in php, and they used to not care about the exact types of a variable as long as it can be coerced into the expected type. If you think it over, probably 80-90%+ of all incoming data we are working with coming from a source which delivers everything as a string (anything coming from HTTP is a string, anything coming from the database which isn't using a binary protocol arrives as string, anything coming from memcached is a string, redis: same thing). But php doesn't care, because one of it's distinguished features is that it can coerce between scalar types. If we introduce scalar type hints, people will use it, if they will use it, people have to be aware that they can't pass numeric looking strings into functions and expect them to work. It will blow up with a fat E_RECOVERABLE_ERROR. But this won't hold back the library authors from using it, which means that the consumers of those libs has to start coding defensively, and make sure that the variable holding the value 123 is an integer and not a float or a string, because that will blow up something. And we can argue whether or not it is a good thing that php has type juggling, but we have that, and it is not likely to be changed(biggest BC break ever), so introducing something which completelly negates that won't likely to get support from the devs. --=20 Ferenc Kov=C3=A1cs @Tyr43l - http://tyrael.hu --001a11c27caa5ea84e04fe3a6cff--