Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58799 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23315 invoked from network); 9 Mar 2012 02:29:58 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Mar 2012 02:29:58 -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:63716] helo=mail-tul01m020-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2F/F1-10820-52B695F4 for ; Thu, 08 Mar 2012 21:29:57 -0500 Received: by obbwd1 with SMTP id wd1so1582429obb.29 for ; Thu, 08 Mar 2012 18:29:55 -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=9SHdXIzDRRFAoKv8LXF5yX/D6C4dCQAZdFG5jdpS1A4=; b=pkkwjpCOJOyD/2uS9aUWuXXMRXElcX8EBQHh1H8PV3E1+XVAZM587mURPRQUQv64xm SDAUGSVpgimBjzk2G+7vnGVMLwILFGUHFcDJf+erCOnLZu4n1mf91c0pU2bhdRhKSPY+ X/eM0d3w9uWrP//VT5yd38LEw3gSNUAJYqnyMgkPcSylxvJf8e5IyabUvudaIhhiVGbu HHsaQ61xMl+WaxpTXzgHeJedEFxp3IixuOK9x6I0IPfyY3zFYBjooUhjCL9IEBeCOLr8 VMrc9lnyHI5QncCUrRBmHR7KEnxrct/F0kDjgxkujykBeoJreHZcY/xyTsjMtwyfcGv2 bmgg== MIME-Version: 1.0 Received: by 10.182.38.7 with SMTP id c7mr194794obk.44.1331260195054; Thu, 08 Mar 2012 18:29:55 -0800 (PST) Received: by 10.182.19.104 with HTTP; Thu, 8 Mar 2012 18:29:54 -0800 (PST) In-Reply-To: References: <52.38.15021.3A1E65F4@pb1.pair.com> <4F570EBB.6030103@sugarcrm.com> <60744d7ea642fe03c7110502a3f4276a@mohiva.com> Date: Thu, 8 Mar 2012 21:29:54 -0500 Message-ID: To: John Crenshaw Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=f46d0447f28c07a29004bac62b70 Subject: Re: [PHP-DEV] Scalar Type Hinting From: adamjonr@gmail.com (Adam Jon Richardson) --f46d0447f28c07a29004bac62b70 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Mar 8, 2012 at 6:08 PM, John Crenshaw wrote: > > > Well, if your type hints gets more forgiving, than it's the same that was > > proposed by this function a((int) $arg) {} And in this case hints have no > > meaning at all - it's just other syntax to do the conversion that now > looks > > like this function a($arg) { $arg = (int)$arg; } > > > > That's black and white thinking. Casting is obviously too forgiving (a > cast literally accepts ANYTHING), and there were specific problems > identified with that syntax that have absolutely nothing to do with whether > you can juggle parameter types. > > Current sentiment seems to be settling around behavior similar to the > internal zend_parse_parameters, which is to say: > (function(int){})('1'); // no error > (function(int){})('123xyz'); // E_NOTICE, function gets (int)1 > (function(int){})('xyz'); // E_WARNING, function gets (int)0 > I don't think it's fair to say that casting is too forgiving OR that current sentiment is settling around behavior similar to the internal zend_parse_parameters. This is a complex issue, and there are many core developers who have been focusing on other things besides this discussion. Their silence likely does not demonstrate sentiment of approval for anything at this point. This particular subject has much history on the PHP mailing list, and the opinions are far ranging. More importantly, this general subject has much history in all of the programming languages that are dynamically typed. One can find similar discussions regarding Ruby, Python, and general discussions of type: http://www.codecommit.com/blog/ruby/adding-type-checking-to-ruby http://stackoverflow.com/questions/734368/type-checking-of-arguments-python http://journal.stuffwithstuff.com/2010/08/31/type-checking-a-dynamic-language/ And, while there are some dynamically typed languages that allow optional types, they don't provide much help here, either: - Clojure allows type hints, but they're just for performance reasons. http://clojure.org/java_interop - Dart has optional types, but in Dart, everything is an object AND it's types are unsound (i.e., let you get away with lots): http://www.dartlang.org/articles/why-dart-types/ My point is that there are many beliefs about how to handle optional type checks on function arguments in dynamic, weakly typed languages, and there's not much precedent. That's not to say that nothing should be added to PHP's capabilities. However, one constant question that emerges when considering types in dynamic languages is what does it really buy you? The value has to be significant, especially considering the lack of a compiler that can ensure the correctness of a type before runtime. As Stas pointed out in the thread for the Enun RFC (noting the importance of a compiler): One note here: Java is a statically typed compiled language. PHP is not. So > many arguments regarding type safety, etc. which are completely valid in > regards to Java have no validity in regard to PHP. I understand that some > people want to import some bits from strictly typed languages, imagining > that this will provide them with benefits that statically typed languages > have, namely compile-time safety checks, etc. This does not work as well in > dynamic languages. Note for example that neither Ruby nor Python have > enums, though you can do similar things there. Let's look at the examples you gave: (function(int){})('1'); // no error (function(int){})('123xyz'); // E_NOTICE, function gets (int)1 (function(int){})('xyz'); // E_WARNING, function gets (int)0 I'm wondering where you would be getting the values '123xyz' and 'xyz' from at runtime that you're passing off to a function expecting ints? If these come from GPC's, then wouldn't an app pick up the issue BEFORE getting to the point that they're being used as arguments through proper input validation. And, if these come from a MySQL database, then wouldn't they have been stored in a field that only stores some type of int (otherwise one wouldn't be passing them to a function expecting ints.) And, if it comes from a file, wouldn't the input be validated there, too, before calling this function? Now, I'm not saying there are no examples, but they would appear to be very limited in applications. Joe Armstrong, creator of the dynamically typed Erlang language, advocates performing checks on the datatypes at the points in the application that receive input, but not within the internal functions. It's an interesting view, and he seems to have had success building robust, stable software using this approach. Now, I'm not saying that this should be PHP's approach. I recently presented the idea of having a scalar type (with aliases) because this could identify problems the first time a PHP file is parsed rather than at runtime. However, I do bring up Joe Armstrong's approach to temper the belief that casting or something less is "too forgiving." I am interested in adding some type of ability to PHP to catch issues at parse time rather than runtime. And, I very much enjoyed seeing someone as skilled as Anthony crank out a quick proof-of-concept (I'd still be sifting through Zend's internals trying to get out of my Objective-C mode of thinking.) I just hope that we look at all options (including doing nothing), consider the research already done on dynamic type systems, and look to examples in other programming languages that can give us insights into what works well and what doesn't. Adam --f46d0447f28c07a29004bac62b70--