Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58532 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 96182 invoked from network); 2 Mar 2012 20:15:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Mar 2012 20:15:47 -0000 Authentication-Results: pb1.pair.com header.from=adamjonr@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=adamjonr@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: adamjonr@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-tul01m020-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:64293] helo=mail-tul01m020-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 04/9F-22821-27A215F4 for ; Fri, 02 Mar 2012 15:15:47 -0500 Received: by obbwd1 with SMTP id wd1so2611890obb.29 for ; Fri, 02 Mar 2012 12:15:44 -0800 (PST) Received-SPF: pass (google.com: domain of adamjonr@gmail.com designates 10.182.231.100 as permitted sender) client-ip=10.182.231.100; Authentication-Results: mr.google.com; spf=pass (google.com: domain of adamjonr@gmail.com designates 10.182.231.100 as permitted sender) smtp.mail=adamjonr@gmail.com; dkim=pass header.i=adamjonr@gmail.com Received: from mr.google.com ([10.182.231.100]) by 10.182.231.100 with SMTP id tf4mr4376452obc.56.1330719344472 (num_hops = 1); Fri, 02 Mar 2012 12:15:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=ZnbL378BoxMJu8CjGJ18xqfrBU4VTfL8M/AQ2mG0z5I=; b=BVL5lk8+44zPULlv2jK0RliPXjdA1gnBfIAdb6DO7aOXAZMhPgg7Zi2MyF2xbsKY6C NeKG50aeioOtQN59ihawexbwXyhF9FrvssEdy4gDNZRe7Nrq90o4+4mm1DS8Duqxmvf5 kx6AsWKKdgQqg6dOuXGGIHJ8F+JDSDMnHw+7nxOcACZ93i24QIrpIa4uuQPvqvo0XFwm XTxbx6erP+lB6XD4nCDr0NSJaatM9YXTYvy/LydMrKiib/bY6FkZkgwTQ9fV3HJ9tvkL 8hMgu41QM0mTOubi9U8lHBOohiF92/XmQq1Z531ZE7svWQ5wyte7U5yoc80yLUXoludD NP5Q== MIME-Version: 1.0 Received: by 10.182.231.100 with SMTP id tf4mr3793092obc.56.1330719344384; Fri, 02 Mar 2012 12:15:44 -0800 (PST) Received: by 10.182.19.104 with HTTP; Fri, 2 Mar 2012 12:15:44 -0800 (PST) In-Reply-To: References: Date: Fri, 2 Mar 2012 15:15:44 -0500 Message-ID: To: Anthony Ferrara Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d04479615d15aee04ba483d50 Subject: Re: [PHP-DEV] [POC Patch] Scalar Type Hinting/Casting - Proof Of Concept From: adamjonr@gmail.com (Adam Jon Richardson) --f46d04479615d15aee04ba483d50 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Mar 2, 2012 at 7:51 AM, Anthony Ferrara wrote: > Well, there are a few questions about the implementation: > > 1. *Which* type casting rules should it follow? > > a. Regular cast rules (like $foo = (int) $foo), where it converts > always without error? > b. Internal function cast rules, where it warnings on error and > prevents execution of the function. > c. Current type hinting rules, where if it can't convert cleanly it > E_RECOVERABLE_ERRORS > > Personally, I like C the best. Where if it is passed an invalid > value, it attempts to cleanly convert, but errors out if it can't... > But I can see other arguments being made... > > 2. Should (array) be supported? Perhaps. So at that point, foo(array > $bar) would do a "strict" check, and foo((array) $bar) would attempt > to cast. But my question would be: what would attempt to cast mean? > Should it error out if you pass foo(1)? That's what the internal > function cast rules do. And to me that's more obvious than silently > converting it to foo(array(1))... > > 3. Should references be supported? My feeling is yes, they should. > So if you do foo((array) &$bar), it would cast the original value (if > possible) as well. > > 4. What about consistency? Well, there currently is no consistency. > Internal function parameters behave one way, and explicit casts behave > another. And even more confusing implicit casts behave yet another > way ($a + $b). So to implement this, we'd need to be consistent with > one of them. Frankly, I would only want to add consistency to > internal function parameters, since the explicit cast is not useful > IMHO (it's identical to $bar = (int) $bar), at which point it's not > worth adding to save only that one line. But if we're consistent with > internal function parameter checking, then it becomes quite useful. > We can throw warnings on unclean conversion and prevent execution of > the function... That way, all function calls behave the same (as much > as I hate the concept of warnings on type hint failure)... So, in > that case, function calls become an implicit cast to the type, which > is then why the stricter error handling (without breaking the spirit > or consistency). > > 5. What about BC breaks? Well, this entire patch (up to this point) > wouldn't require one. it's only adding the casting functionality > (which is not implemented today), so no problem. Existing code would > still function fine. > > Thoughts? Should I update the patch to be more inline with what I > said above (the implicit hints that are offered by the current > internal function argument mechanism: > > # sapi/cli/php -r 'function foo((int) $bar) { return $bar; } $a = "1"; > var_dump(foo($a));' > int(1) > > # sapi/cli/php -r 'function foo((int) $bar) { return $bar; } $a = > "foo"; var_dump(foo($a));' > > Warning: Argument 1 passed to foo() must be of the type integer, > string given, called in Command line code on line 1 and defined in > Command line code on line 1 > > However, since it's not raising a terminating error, more changes > would need to be made to the VM to check the return status of the > argument check (which is currently ignored) to determine if to proceed > with the function call, or just return null imediately... > > Thoughts? Well, this seems like a reasonable approach (at least in terms of general discussion.) I would suggest option a), for if it looks like the same type of cast found within the body of functions. I believe users will expect it to act the same way, too. Keeping track of two different cast behaviors would add to the likelihood of misusing one or the other. Additionally, previous proposals have struggled due to the impedance mismatch perceived between the proposed hinting solutions and PHP's intrinsic typing qualities. Keeping things close to the current modus operandus seems like it gives the proposal more chance of becoming a reality. I would also suggest that array not be included, if only to limit the scope of the current proposal and simplify the offering. It could always be added later. However, if it led to an increased likelihood of being considered/passed for some reason, then include it :) Nice work, Anthony. I am curious what some of the core developers who've been opposed to scalar type hinting in the past would think of this approach. Zeev, Stas, others, would this be worth any consideration? It seems like an approach that is potentially more consistent with PHP's typing mechanisms. Thoughts? Adam --f46d04479615d15aee04ba483d50--