Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:57025 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 57440 invoked from network); 22 Dec 2011 19:49:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Dec 2011 19:49:15 -0000 Authentication-Results: pb1.pair.com header.from=johncrenshaw@priacta.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=johncrenshaw@priacta.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain priacta.com designates 64.95.72.243 as permitted sender) X-PHP-List-Original-Sender: johncrenshaw@priacta.com X-Host-Fingerprint: 64.95.72.243 mxout.myoutlookonline.com Received: from [64.95.72.243] ([64.95.72.243:3595] helo=mxout.myoutlookonline.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/20-12618-AB983FE4 for ; Thu, 22 Dec 2011 14:49:14 -0500 Received: from mxout.myoutlookonline.com (localhost [127.0.0.1]) by mxout.myoutlookonline.com (Postfix) with ESMTP id 2E49B416D55; Thu, 22 Dec 2011 14:49:11 -0500 (EST) X-Virus-Scanned: by SpamTitan at mail.lan Received: from HUB012.mail.lan (unknown [10.110.2.1]) by mxout.myoutlookonline.com (Postfix) with ESMTP id 09961416EF1; Thu, 22 Dec 2011 14:49:08 -0500 (EST) Received: from MAILR001.mail.lan ([10.110.18.27]) by HUB012.mail.lan ([10.110.17.12]) with mapi; Thu, 22 Dec 2011 14:49:06 -0500 To: Rasmus Lerdorf , Dmitri Snytkine CC: 'Sebastian Bergmann' , "internals@lists.php.net" Date: Thu, 22 Dec 2011 14:49:03 -0500 Thread-Topic: [PHP-DEV] Return Type Hinting for Methods RFC Thread-Index: AczA4JkOppEqKzJsRViKqHcVUypIxQAAHW7Q Message-ID: 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: <4EF385AE.8000103@lerdorf.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: RE: [PHP-DEV] Return Type Hinting for Methods RFC From: johncrenshaw@priacta.com (John Crenshaw) From: Rasmus Lerdorf [mailto:rasmus@lerdorf.com]=20 > How is that hard? > > function age_lookup($age) { > return $coll->find(array('age' =3D> (int)$age); > } > > but again here, doing a strong type check on the parameter isn't making y= our life easier. It simply pushes the responsibility to the caller and intr= oduces a tricky unrecoverable error that will drive you crazy unless you ha= ve 100% regression test coverage (which is kind of impossible since the num= ber of inputs is infinite) or great static analysis tools. PHP is not a compiled language, so you end up not catching these until runt= ime which is obviously sub-optimal. > -Rasmus This will silently fail in a very bad way when the caller accidentally pass= es 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 (eve= n just a decent IDE) could highlight the error even before it is executed. The point here is that the caller is already responsible to make sure it pa= sses the right value, but the language currently offers no means of documen= ting that, and no easy way to ensure it. There's obviously a question about how much juggling to allow with a scalar= type hint. IMO the obvious stuff ("36" to 36, etc.) is perfect, but passin= g "foobar" or array(1,2,3) to an int should throw an error. John Crenshaw Priacta, Inc.