Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58800 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24628 invoked from network); 9 Mar 2012 02:32:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Mar 2012 02:32:16 -0000 Authentication-Results: pb1.pair.com header.from=ircmaxell@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ircmaxell@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) X-PHP-List-Original-Sender: ircmaxell@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qy0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:62872] helo=mail-qy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3F/42-10820-0BB695F4 for ; Thu, 08 Mar 2012 21:32:16 -0500 Received: by qcmt36 with SMTP id t36so906798qcm.29 for ; Thu, 08 Mar 2012 18:32:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=8zCdG2orpS205TuNZ11aNJD5fg1HyZGaHeciVXJWOoE=; b=ul+QS6KwcyxLiC0Cehfi0z1fYe77kn2dxFPYX6zkHgyGfzrUhP09pt3anBb5Gxw/X1 ZITyIm0eq1OWInoKSNlU6GaCDOmzh8843zmxIH+5s7Lb5OS20o35stbqujA+SSbCtvJY JKExqOoDyoFF5u0aTZY0OjPXcQwg6ooMvBwB0XvpLh+3f1jKbKhrZmyuNj/r+WhQCT4F UV11zE9C2KdksF1n+DIMLNpC/aci3s+RWYl1+uJvrLnMNxRKLIVOoxr4Q0+wdVkfIbst jhOAOJHShSRjvkuJR6Hme8Cm4+sO1CceNk7gsUIOXiZIOekdxEFFuP2TFSQEtzN5vXZL GcpQ== MIME-Version: 1.0 Received: by 10.224.207.70 with SMTP id fx6mr229833qab.79.1331260332845; Thu, 08 Mar 2012 18:32:12 -0800 (PST) Received: by 10.229.49.74 with HTTP; Thu, 8 Mar 2012 18:32:12 -0800 (PST) Date: Thu, 8 Mar 2012 21:32:12 -0500 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: [POC - Patch] Scalar Type Hinting - A-La zend_parse_parameters From: ircmaxell@gmail.com (Anthony Ferrara) Hey all, As promised, I've created a POC patch to implement scalar type hints, the way that zend_parse_parameters handles hinting. First off, here's the patch: Directly apply-able without re2c: https://gist.github.com/2004623 Removing generated files (requires re2c to compile): https://gist.github.com/2004650 It's a POC, but it mostly works. There is one known issue: passing a class implementing __toString to a string hinted function will raise a segmentation fault. There's also an issue of not separating the argument on cast yielding to reference whether indicated or not, but that should be easy to fix (it's just issuing a SEPARATE_IF_NOT_REF in the cases where it would cast)... I'll work on cleaning it up, but I wanted to show the concept before investing too much work... So, basically, there are 4 new parameters: bool int float string The casting vs error rules are identical to zend_parse_parameters. So: function fooi(int $i) { var_dump($i); } fooi(1); // int(1) fooi(1.5); // int(1) fooi("1"); // int(1) fooi("1abc"); // int(1) + notice about non-well-formed numeric fooi("foo"); // E_RECOVERABLE_ERROR fooi(true); // int(1) fooi(array()); // E_RECOVERABLE_ERROR fooi($obj); // E_RECOVERABLE_ERROR function foob(bool $b) { var_dump($b); } foob(1); // bool(true) foob(1.5); // bool(true) foob("1"); // bool(true) foob("abc"); // bool(true) foob(true); // bool(true) foob(array()); // E_RECOVERABLE_ERROR foob($obj); // E_RECOVERABLE_ERROR function foos(string $s) { var_dump($s); foos(1); // string("1") foos(1.5); // string("1.5") foos("1"); // string("1") foos(true); // string("1") foos(array()); // E_RECOVERABLE_ERROR foos(new StdClass); // E_RECOVERABLE_ERROR foos($objImpl__toStringORcast_object); // string(result) Float works like int, so I won't list it out here... So, what do you think? Thanks, Anthony