Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57015 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40669 invoked from network); 22 Dec 2011 19:12:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Dec 2011 19:12:32 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 209.85.161.170 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.161.170 mail-gx0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:41342] helo=mail-gx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2B/5C-12618-F1183FE4 for ; Thu, 22 Dec 2011 14:12:31 -0500 Received: by ggnv1 with SMTP id v1so7000971ggn.29 for ; Thu, 22 Dec 2011 11:12:28 -0800 (PST) Received: by 10.50.88.170 with SMTP id bh10mr10062002igb.8.1324581148478; Thu, 22 Dec 2011 11:12:28 -0800 (PST) Received: from [192.168.200.5] (c-50-131-44-225.hsd1.ca.comcast.net. [50.131.44.225]) by mx.google.com with ESMTPS id j3sm31978428ibj.1.2011.12.22.11.12.27 (version=SSLv3 cipher=OTHER); Thu, 22 Dec 2011 11:12:27 -0800 (PST) Message-ID: <4EF3811A.7080100@lerdorf.com> Date: Thu, 22 Dec 2011 11:12:26 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 MIME-Version: 1.0 To: Sebastian Bergmann CC: internals@lists.php.net, Dmitri Snytkine References: <2095305E-D4E3-4D7E-8218-32EE99688E0C@GMAIL.COM> <2C90FB94-38C4-4270-8C6A-B89304BA8ED8@gmail.com> <159A7CA2-8561-40DA-9434-CAAE12304DDB@gmail.com> <005701ccc0b3$58c8dee0$0a5a9ca0$@alliantinternet.com> <20111222145159.GY25857@alliantinternet.com> <006101ccc0ba$46b81160$d4283420$@alliantinternet.com> <4EF379D8.9000206@lerdorf.com> <4EF37C29.1010206@php.net> In-Reply-To: <4EF37C29.1010206@php.net> X-Enigmail-Version: 1.4a1pre Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Return Type Hinting for Methods RFC From: rasmus@lerdorf.com (Rasmus Lerdorf) On 12/22/2011 10:51 AM, Sebastian Bergmann wrote: > Am 22.12.2011 19:41, schrieb Rasmus Lerdorf: >> This is not a step forward. If the author of age_check() really doesn't >> want to accept type-juggled arguments, then it is easy enough to do a >> strict type check in the function itself. This puts the effort in the >> correct place and doesn't encourage this type of coding. > > Putting such code into the "correct" place does not change the problem > that you and Stas describe > > function age_check($age) > { > if (!is_int($age)) { > throw new InvalidArgumentException; > } > } > > With the above code, the caller needs to cast and the writer of the > age_check() function has to copy/paste/adapt these checks to all the > correct places ... > > I am not advocating type hints for scalars, I am just saying that this > argument is not really a good one against it. But people don't really write code like this in PHP. Which is also why it makes very little sense to add strong typing of scalars. Why would anyone want to add a feature that lets them do something they would never do? The above code is much more likely to do an is_numeric() check and/or a hard typecast to an int. But throwing an exception when it receives "21" instead of 21 just isn't something people tend to do. And Dmitri, in the Mongo case you mentioned, parameters to Mongo need to be json-wrapped, so you are going to have access functions doing this. Why would you not want to typecast in the access function there as well as opposed to throwing hard to catch exceptions going into the access functions themselves? The only way I see something like this ever happening in PHP is if we came up with an intelligent approach that actually was type *hinting* and not strong typing. As in: function ((int)$age) { return ($age > 21) ?: false; } that would gracefully handle interchangeable types. But it gets tricky once we start thinking about non-numeric strings being passed in there. -Rasmus