Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58466 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27947 invoked from network); 2 Mar 2012 10:27:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Mar 2012 10:27:42 -0000 Authentication-Results: pb1.pair.com header.from=linepogl@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=linepogl@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.160.170 as permitted sender) X-PHP-List-Original-Sender: linepogl@gmail.com X-Host-Fingerprint: 209.85.160.170 mail-gy0-f170.google.com Received: from [209.85.160.170] ([209.85.160.170:32846] helo=mail-gy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 10/99-11220-C90A05F4 for ; Fri, 02 Mar 2012 05:27:41 -0500 Received: by ghbg2 with SMTP id g2so768421ghb.29 for ; Fri, 02 Mar 2012 02:27:38 -0800 (PST) Received-SPF: pass (google.com: domain of linepogl@gmail.com designates 10.236.154.168 as permitted sender) client-ip=10.236.154.168; Authentication-Results: mr.google.com; spf=pass (google.com: domain of linepogl@gmail.com designates 10.236.154.168 as permitted sender) smtp.mail=linepogl@gmail.com; dkim=pass header.i=linepogl@gmail.com Received: from mr.google.com ([10.236.154.168]) by 10.236.154.168 with SMTP id h28mr12334779yhk.59.1330684058509 (num_hops = 1); Fri, 02 Mar 2012 02:27:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=h5PIepu0wIr8l32o6VJSQr1N/Qu3pV8g8AHQVSLjfVY=; b=c4E3EIz6RA+htaebzmJfDUydpqosqgqsVSLY8lo/ivYEfKg5wWmeng/mwMR6d1R7Db hFAtYUQlrEZvf//yZdUr7XxdMfDxdIc6b/fVJtDov3qa3wwqiHURBuHnC7JreO9CcWOs HYIYnK16Uh/GCFGfq3lFyVW79D9kYSNeiBfkwfKkpWHKM5rz4QmoOTuJ2aagndp9QqtJ vFAfTrpdZSKVQg7SLH5AGwpP72g/E8SKxuwHooGGXNeQTlJLYkViPF0WxgdMQvcJFgIy r+cywkv33hfLTDo9flW7y7b1kmIYAShgopOSVGB0a/t4jJTu/vhhiW0ecNoCMbdB3RN/ QQug== Received: by 10.236.154.168 with SMTP id h28mr9760370yhk.59.1330684058246; Fri, 02 Mar 2012 02:27:38 -0800 (PST) MIME-Version: 1.0 Received: by 10.147.125.8 with HTTP; Fri, 2 Mar 2012 02:27:18 -0800 (PST) In-Reply-To: References: Date: Fri, 2 Mar 2012 11:27:18 +0100 Message-ID: To: Anthony Ferrara Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=20cf3040eb9a999cd504ba4006e4 Subject: Re: [PHP-DEV] [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept From: linepogl@gmail.com (Lazare Inepologlou) --20cf3040eb9a999cd504ba4006e4 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable +1 for the syntax. There are two nice side effects I would like to underline. 1. Error-raising can be clearly delegated to the type juggling mechanism. There will be no need to implement anything new here but reuse the existing type juggling system of PHP. That would be very consistent. At the end of the day, these two syntaxes are completely equivalent: function foo((int) $bar) { ... } function foo($bar) { $bar =3D (int)$bar; ... } 2. If we put passing by reference into the picture, it is easy to see why the following syntax should be a parsing error: function foo((int) & $bar) { } // parsing error On the contrary, were there no brackets, the resemblance to the syntax of C would be confusing. Lazare INEPOLOGLOU Ing=C3=A9nieur Logiciel 2012/3/2 Anthony Ferrara > Hey all, > > I know given all the discussion about this topic lately, this is a hot > topic. But I whipped together a quick POC patch to implement scalar > type casting for function parameters. Let me describe it: > > Patch: https://gist.github.com/1947259 > > Example: > > function foo( (int) $bar ) { > var_dump($bar); > } > > foo(1); // int(1) > foo("1"); // int(1) > foo(1.5); // int(1) > foo("foo"); // E_RECOVERABLE_ERROR - Expected integer > foo(array()); // E_RECOVERABLE_ERROR > > Right now, I only implemented the checks for (int), but I add the > parser constructs for (int), (float), (bool), (string) and (object)... > > Now, let's talk why I did what I did: > > Why did I use cast syntax? Well, there are really three main reasons. > First off, to indicate that a cast may happen. Second, to prevent > needing new tokens (and hence reserved words). And third to provide a > distinction between a string class type hint and a string scalar type > hint. > > Why did I only implement (int)? Well, because I just wanted to build > a quick dirty POC that can be executed to see the semantics of > operation. There are issues with it now, so rather than doing all the > work to re-do it later, I just implemented int... > > Why implement (object)? Because right now, there's no way to say you > want to accept a generic object without caring about type. So the > (object) cast/hint would then provide that ability to accept a generic > object. > > Why not implement (resource)? Because that would require a new parser > token, as it's not available now... > > How does the casting work? Right now, it's using a copy of the same > rules that internal functions use with zend_parse_parameters. That > way, it brings the operating semantics of internal functions and > userland functions more inline with each other. > > > > So with that said, there are some (significant) issues with the patch: > > 1. First off, the arg checks happen before separation of the zval on > non-referenced calls. So that means the cast effects the original > zval AND the argument. Which is a no-go for a production patch. So > that means that the cast logic would need to be put after the zval > split. But we'd still want the checks first, so it's not too > difficult to segregate, just requires deeper changes. It's not > difficult (that I can see yet), just more work... Example of the > problem: > > # sapi/cli/php -r 'function foo((int) $bar) { var_dump($bar); } $a =3D > "1"; foo($a); var_dump($a);' > int(1) > int(1) > > 2. Right now, the zend_aprse_arg_impl ( > http://lxr.php.net/xref/PHP_5_4/Zend/zend_API.c#zend_parse_arg_impl ) > that's used by internal functions is defined as static. So we'd be > copying a lot of the code back and forth. In the production patch, > I'd also want to re-factor that out a bit into either functions or > macros to handle the type conversion and casting in both places. That > way, both systems would behave identical (or as close as possible). > > > So, with that said, what do you think? Is this something worth > pursuing? Are there any fundamental issues that I'm missing? What > else would we need to cover in a production patch and RFC? > > Thanks, > > Anthony > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --20cf3040eb9a999cd504ba4006e4--