Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57028 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 62681 invoked from network); 22 Dec 2011 19:56:50 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Dec 2011 19:56:50 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@lerdorf.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@lerdorf.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain lerdorf.com from 209.85.210.170 cause and error) X-PHP-List-Original-Sender: rasmus@lerdorf.com X-Host-Fingerprint: 209.85.210.170 mail-iy0-f170.google.com Received: from [209.85.210.170] ([209.85.210.170:47415] helo=mail-iy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 90/51-12618-18B83FE4 for ; Thu, 22 Dec 2011 14:56:49 -0500 Received: by iafj26 with SMTP id j26so15203565iaf.29 for ; Thu, 22 Dec 2011 11:56:46 -0800 (PST) Received: by 10.42.154.7 with SMTP id o7mr12371455icw.48.1324583806859; Thu, 22 Dec 2011 11:56:46 -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 h9sm32245439ibh.11.2011.12.22.11.56.45 (version=SSLv3 cipher=OTHER); Thu, 22 Dec 2011 11:56:46 -0800 (PST) Message-ID: <4EF38B7C.8020900@lerdorf.com> Date: Thu, 22 Dec 2011 11:56:44 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 MIME-Version: 1.0 To: John Crenshaw CC: Dmitri Snytkine , 'Sebastian Bergmann' , "internals@lists.php.net" 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> <4EF3811A.7080100@alliantinternet.com> <007f01ccc0de$b578d750$206a85f0$@alliantinternet.com> <4EF385AE.8000103@lerdorf.com> In-Reply-To: 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 11:49 AM, John Crenshaw wrote: > From: Rasmus Lerdorf [mailto:rasmus@lerdorf.com] > >> How is that hard? >> >> function age_lookup($age) { >> return $coll->find(array('age' => (int)$age); >> } >> >> but again here, doing a strong type check on the parameter isn't making your life easier. It simply pushes the responsibility to the caller and introduces a tricky unrecoverable error that will drive you crazy unless you have 100% regression test coverage (which is kind of impossible since the number of inputs is infinite) or great static analysis tools. > PHP is not a compiled language, so you end up not catching these until runtime which is obviously sub-optimal. > >> -Rasmus > > This will silently fail in a very bad way when the caller accidentally passes in (for example) an array. With a scalar type hint it would die loudly (quickly alerting the developer to the problem) and code analysis tools (even just a decent IDE) could highlight the error even before it is executed. That's an argument for "scalar" as the type hint, not for "int" vs. "string". I'm not against "scalar" as a type hint. I am against hints that prevent interchangeable types from being passed. scalar, array, object, callable are fine. -Rasmus