Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81625 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 1608 invoked from network); 2 Feb 2015 18:22:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2015 18:22:44 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.178 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.220.178 mail-vc0-f178.google.com Received: from [209.85.220.178] ([209.85.220.178:61767] helo=mail-vc0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 32/4E-34915-370CFC45 for ; Mon, 02 Feb 2015 13:22:44 -0500 Received: by mail-vc0-f178.google.com with SMTP id hy10so15215933vcb.9 for ; Mon, 02 Feb 2015 10:22:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=d65aGk8Ce7tde0J58HSJtkwCACTbO24sz+/4vHy3gvU=; b=XHR7CGDI2EaDwVGCyJ/6BH6pzLzswo48MlltXmrpZuocVFNo014je2vFRu1Ad3JrKD ilVQDW+O7H+YKuUZSTT/McyBNK+rWqMNAij4wf32Dp/Rpk2/1KTLNNArS44AHmY9SB+f yLVjBen4S/JZ5dWBIPIXrCmqVjVpgPvPD4cHwpi7sN9AhkGtqspZDem1vNapNC70n+38 4qt1/wwAoZUEV4Dg8M7DLJG5x+NzjJYXBdjl0KyKOqjkJCpSJV47HasW92PiqkHYo59p Lszx5Lq5eoSp7SF/EIRt3tgXV4YKMfu7WvT5UX44NZOqTeJMGcFFXb1ompdBit/csXvo 022A== X-Gm-Message-State: ALoCoQkZfahk1q7GGR6liWdyunDfLSi7yi/s+PKr2vUxWC4E8aGsV3FihH7eyjGySd5ITx3Sob99Ahge0MOE0mGMjkQh9TnUpdtBRWREymV94jmssp2iWcDNCCkGGu1Z+a0zkDjDHxhlCnH4U0mSIzuDl55kw+fZ5w== MIME-Version: 1.0 X-Received: by 10.52.103.75 with SMTP id fu11mr11066888vdb.5.1422901361202; Mon, 02 Feb 2015 10:22:41 -0800 (PST) Received: by 10.52.74.73 with HTTP; Mon, 2 Feb 2015 10:22:41 -0800 (PST) In-Reply-To: <5175DE01-ADC9-4A0B-8343-820C53B30054@ajf.me> References: <8DCD1B72-C81D-499E-B455-E4A042CD76E6@ajf.me> <1E54E93F-8CE1-469F-BE1F-DD2F1DF76E39@ajf.me> <5175DE01-ADC9-4A0B-8343-820C53B30054@ajf.me> Date: Mon, 2 Feb 2015 22:22:41 +0400 Message-ID: To: Andrea Faulds Cc: PHP Internals List Content-Type: multipart/alternative; boundary=047d7b86d9902f6d54050e1f0ae7 Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2 From: dmitry@zend.com (Dmitry Stogov) --047d7b86d9902f6d54050e1f0ae7 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable So, one of the advantages of strict type hinting is the ability to catch potential errors. For example when we pass 67 to setBody(string $message). Thanks. Dmitry. On Mon, Feb 2, 2015 at 7:30 PM, Andrea Faulds wrote: > Hi Dmitry, > > > On 2 Feb 2015, at 09:24, Dmitry Stogov wrote: > > > > > Will we able to call the same function using weak > > > type hinting from on file and with strict from the other? > > > > Yes, for the parameter type hints anyway. That means that strict and > weak code is interoperable without enforcing a model on each other. > > > > At first I thought, this is ugly solution. Now I'm not completely sure. > > I did as well when I first saw the idea proposed (someone else originally > suggested it). It does seem ugly on the surface, but it has some > advantages. Sure, you have to add this extra line of code to each file, > that=E2=80=99s annoying. But, it means full backwards-compatibility, easi= er > migration of existing codebases, and most importantly, allows people to > choose one mode or another without affecting other users that have to cal= l > their code. > > > I see, but this would require declare(strict_types=3D1) everywhere that > would turn PHP into Java. > > Well, not quite Java, but yes. Typing it out is annoying. But it=E2=80=99= s better > than implicit strict typing for a directory, and it means we avoid having > two or three different kinds of scalar type hint, so there=E2=80=99s no m= ixing of > systems within the same file. > > After a while I think people will get used to it. PHP programmers already > have to type out " programmers need "#!/usr/bin/perl=E2=80=9D and =E2=80=9Cuse warnings; use= strict=E2=80=9D, JS > programmers need =E2=80=9C(function () { =E2=80=98use strict=E2=80=99; }(= ));=E2=80=9D etc. Also, IDEs would > help. > > > > Strict type hinting is not suitable for PHP by definition (as a weakl= y > > > typed language), however, I see, it may be useful in some cases. > > > I would prefer to have "weak" types at first, then think about > introducing > > > ability to switch to "strict" type hinting in context of use-cases. > > > > That'd be possible, but I fear that we'd just end up with weak typing > only and no strict solution. Regardless of its merits, a large portion of > the community is in favour of a strictly-typed solution. There are also a > lot of people who are in favour of weak typing. So, this RFC tries to mak= e > a compromise. > > > > I see, but I afraid that compromise is worse than one or the other. > > I=E2=80=99m actually convinced that this compromise is in some ways bette= r than > one or the other. > > One of the nice things about strict mode only applying to code which asks > for it, is that if a function if an API has type hints added, it won=E2= =80=99t > suddenly break calling code that didn=E2=80=99t strictly match types, if = that > calling code uses the default weak mode behaviour (which it will if it wa= s > written pre-PHP 7). > > For example, say I have some sort of Response object that lets you set a > body: > > class Response { > public function setBody($message); > } > > In PHP 5, it=E2=80=99s perfectly fine to pass something that=E2=80=99s no= t a string for > $message: > > $foo =3D new Response; > $foo->setBody(67); > > Absurd example, but this sort of thing does happen in real world code, > often accidentally. > > Now, let=E2=80=99s say we add a string type hint in a new version of the = library: > > interface Response { > public function setBody(string $message); > } > > If PHP=E2=80=99s type hints were always strict, then our existing PHP 5 c= ode from > earlier that took advantage of PHP=E2=80=99s weak typing would now produc= e an error: > > Catchable fatal error: Argument 1 passed to Response::setBody() must > be of the type string, integer given > > This isn=E2=80=99t good - it makes migration of the existing codebase dif= ficult. > > Weak typing solves this problem because it allows conversions. However, > it=E2=80=99s not really good that the code was passing an integer in the = first > place - it should really be passing a string. > > The solution, then, is what this RFC offers. By default, weak typing is > used, so your existing code doesn=E2=80=99t break initially. But once you= =E2=80=99ve added > type hints in a few places, you can switch to strict typing, and that err= or > will be detected and can be fixed. > > This way, we can have stricter types without sacrificing compatibility or > complicating migration. > > Thanks. > -- > Andrea Faulds > http://ajf.me/ > > > > > --047d7b86d9902f6d54050e1f0ae7--