Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80538 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 78224 invoked from network); 15 Jan 2015 14:20:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jan 2015 14:20:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.207 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.207 imap2-2.ox.privateemail.com Received: from [192.64.116.207] ([192.64.116.207:45815] helo=imap2-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 78/5A-14306-F9CC7B45 for ; Thu, 15 Jan 2015 09:20:15 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 6005E8C0082; Thu, 15 Jan 2015 09:20:12 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap2.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap2.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id OsGGz7A3VosX; Thu, 15 Jan 2015 09:20:12 -0500 (EST) Received: from oa-res-26-240.wireless.abdn.ac.uk (oa-res-26-240.wireless.abdn.ac.uk [137.50.26.240]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 3C9DD8C0069; Thu, 15 Jan 2015 09:20:11 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: <54B7BA74.4010708@gmail.com> Date: Thu, 15 Jan 2015 14:20:08 +0000 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <0D1C9DE2-B639-44F7-897B-6F2D46D70387@ajf.me> References: <8DCD1B72-C81D-499E-B455-E4A042CD76E6@ajf.me> <4E2073DE-0951-498C-97BB-DDAC094F11FA@ajf.me> <9a033dd1f223f854e760924d118ab812@mail.gmail.com> <2ae0164cb9b9bf1c974d7a3c60af0466@mail.gmail.com> <6105ea99002e634373c09685310e26a6@mail.gmail.com> <54B7BA74.4010708@gmail.com> To: Rowan Collins X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2 From: ajf@ajf.me (Andrea Faulds) Hey Rowan, > On 15 Jan 2015, at 13:02, Rowan Collins = wrote: >=20 > Zeev Suraski wrote on 15/01/2015 11:56: >> PLUS have the ability to radically >> change how it behaves based on a runtime option. It's so bad that we >> decided more than a decade ago that we want to refrain from ever = introducing >> such elements to PHP again (e.g. magic_quotes_runtime), and I'm not = sure why >> we're even discussing it. >=20 > Just on this point, I do think declare() is potentially less evil than = ini_set() / php.ini. A declare() directive is scoped to a particular = section of code, so library authors shouldn't need to care which setting = their users prefer, only the version they themselves wish to use. This = contrasts completely with things like mbstring.func_overload, which = makes shared library code run differently in different configurations. Yeah, that=E2=80=99s the idea behind it. The hope is that you could have = strictly-typed and weakly-typed code interact seamlessly with minimal = issues. And, for the most part, this should allow that. A large part of the motivation behind this was avoiding the problems = that adding separate strict and weak hints would have. Inevitably, = people who were fans of weak typing would use a weak hint all the time, = and people who were fans of strict typing would use a strict hint all = the time. There would also be the odd few who might mix them, but I = don=E2=80=99t think that would happen terribly much. It=E2=80=99s much easier to deal with this: foo(1.0, 1); // strict $qux =3D bar(=E2=80=9Cfoo=E2=80=9D); // also strict foobar($qux, baz(true)); // also strict or this: foo(1.0, 1); // weak $qux =3D bar(=E2=80=9Cfoo=E2=80=9D); // also weak foobar($qux, baz(true)); // also weak Than it would be to have to deal with this: foo(1.0, 1); // weak $qux =3D bar(=E2=80=9Cfoo=E2=80=9D); // strict foobar($qux, baz(true)); // baz is strict, foobar is weak This RFC would guarantee that, for the most part, you=E2=80=99re dealing = with the first case (strict mode) or the second case (default). I fear = that separate strict and weak hints would result in the third case being = commonplace. I was also worried that if we just added weak hints, people who didn=E2=80= =99t like them would not use them and instead use manual assertions. = Similarly, I think something similar might happen if we just added = strict hints. In both cases, you would be ending up with the third = situation. Also bear in mind that if we added strict hints and did not = change the behaviour of extension and built-in PHP functions, we=E2=80=99d= also be dealing with that third situation, since zend_parse_parameters = essentially does a kind of weak typing. Of course, this RFC does still lead to dealing with both weak and strict = modes sometimes, but at least it=E2=80=99d always be the same mode = within a given file, and by extension, within a given function, method = or class. I think that=E2=80=99s more practical than dealing with two or = more different modes within three lines of code. > As has been clarified elsewhere on this thread, under this proposal a = library author writing function foo(int $foo) will ALWAYS receive an = integer, regardless of how that integer is arrived at. Very = approximately, the declare() directive effectively gives the caller a = choice between two pieces of syntactic sugar, both of which guarantee = that the callee's contract will be met: >=20 > if ( is_int($foo) ) { foo($foo); } else { raise_type_error($foo); } >=20 > or >=20 > foo ( (int)$foo ); >=20 > So, just to repeat, this is NOT like register_globals or = mbstring.func_overload, because shared code never needs to handle both = settings. >=20 > That's not to say I'm 100% convinced that this is the right way to go, = just that it's not as abominable as some people are making out. Right. -- Andrea Faulds http://ajf.me/