Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:58309 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 14396 invoked from network); 28 Feb 2012 23:41:14 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Feb 2012 23:41:14 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmgx.michael@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmgx.michael@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.170 as permitted sender) X-PHP-List-Original-Sender: dmgx.michael@gmail.com X-Host-Fingerprint: 209.85.212.170 mail-wi0-f170.google.com Received: from [209.85.212.170] ([209.85.212.170:62714] helo=mail-wi0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 67/E0-36673-9166D4F4 for ; Tue, 28 Feb 2012 18:41:13 -0500 Received: by wibhj13 with SMTP id hj13so285730wib.29 for ; Tue, 28 Feb 2012 15:41:10 -0800 (PST) Received-SPF: pass (google.com: domain of dmgx.michael@gmail.com designates 10.180.85.35 as permitted sender) client-ip=10.180.85.35; Authentication-Results: mr.google.com; spf=pass (google.com: domain of dmgx.michael@gmail.com designates 10.180.85.35 as permitted sender) smtp.mail=dmgx.michael@gmail.com; dkim=pass header.i=dmgx.michael@gmail.com Received: from mr.google.com ([10.180.85.35]) by 10.180.85.35 with SMTP id e3mr3166443wiz.6.1330472470689 (num_hops = 1); Tue, 28 Feb 2012 15:41:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=eJtTDd+rgqeOW4wOsoshZqEaehdyM98QONo43AyIY2c=; b=nAGQzHUdTtJwUS12KzliJMiHS5ops5GvdY4dc9s2Hzezi+Ht2KBqU6EWBlD4SniEB9 WatPejHuyihtynNPUF4+lCCjAGnnPMEyVF9pTFD6BskgDmNMNimacmE7MYFhL7UVlDST nNmlS/GP0JnZ5mSLPtXot2KsB9UcBoLTJoxx8= MIME-Version: 1.0 Received: by 10.180.85.35 with SMTP id e3mr2589722wiz.6.1330472470602; Tue, 28 Feb 2012 15:41:10 -0800 (PST) Received: by 10.216.30.149 with HTTP; Tue, 28 Feb 2012 15:41:10 -0800 (PST) In-Reply-To: References: <1330357150.2159.30.camel@guybrush> <693e15008681dfe7372eaea66214f8a8.squirrel@www.l-i-e.com> <4F4D5D44.5090307@developersdesk.com> Date: Tue, 28 Feb 2012 18:41:10 -0500 Message-ID: To: PHP Internals List Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Scalar type hinting From: dmgx.michael@gmail.com (Michael Morris) It's better to have it blow up in all cases then. Avoiding the break is tr= ivial int $a =3D (int) 3; And this example illustrates cleanly the difference between this proposal and casting by itself. Introducing this creates a new level of typing, for a total of two. Your proposal creates 3, which is 1 too many. On Tue, Feb 28, 2012 at 6:36 PM, Kris Craig wrote: > But again, that same argument could be used to eliminate the require() > function, which is something that I and many other developers use quite > frequently. > > There are cases where I would want my script to blow-up and stop executin= g, > such as if an included script could not be loaded (hence "require"); or, > given the right circumstances, when my script is relying on a variable th= at, > as a result of some unanticipated behavior, receives an incompatible valu= e > for its type. > > After all, just look at how many PHP scripts out there already do this > manually; i.e. terminating the script (with die() or some other means) if > the variable doesn't pass a bulky and convoluted sanity check.=A0 All tha= t > code could be eliminated by simply having the strong type, since that wou= ld > now be done for you at the lower level.=A0 There are other cases where th= is > sort of conflict would be undesirable but the coder might still want the > script to proceed anyway, hence the need for the weak type.=A0 There are > valid, real-world use cases for both. > > --Kris > > > > On Tue, Feb 28, 2012 at 3:30 PM, Michael Morris > wrote: >> >> I don't see a point in two error levels. =A0Either the coder cares or >> they don't. Further, there are mechanisms already in place. >> >> int $a =3D 'House'; // Warning. >> @ int $a =3D 'House'; // No warning because the suppression operator was >> used. >> >> If you need something more than a warning you can code it. Let's use a >> real world example >> >> @int $id =3D $_POST['id']; >> >> if ($id !=3D $_POST['id']) { >> =A0throw MyValidationException(); >> } >> >> In the language as it stands we have >> >> $id =3D (int) $_POST['id']; >> >> if ($id !=3D $_POST['id']) { >> =A0throw MyValidationException(); >> } >> >> So we have the means to make things secure. =A0The one thing, the only >> thing this is really going to give, is to prevent $a from changing >> type. >> >> To be honest, this isn't useful with solo programmers. =A0Where it will >> be useful is with teams and API enforcement. >> >> This whole strong/weak keywording is a Very Bad Idea(TM) IMHO. =A0Weak >> can already be done with casting. >> >> On Tue, Feb 28, 2012 at 6:17 PM, Kris Craig wrote= : >> > +1 on that. >> > >> > However, I believe the coder should also have the option of determinin= g >> > whether this should be recoverable or not, just like they have a choic= e >> > with >> > regard to using "include" or "require" (one fails gracefully, the othe= r >> > terminates execution). >> > >> > That's where I think strong/weak could be very useful.=A0 Here's how I >> > would >> > do it: >> > >> > weak int $a =3D "House"; // Throws E_WARNING >> > strong int $a =3D "House"; // Throws E_RECOVERABLE_ERROR >> > >> > >> > In both cases, the error can be caught, which would mirror the >> > flexibility >> > in languages like C#.=A0 However, if the weak one isn't caught, the >> > warning is >> > thrown but the script proceeds as normal.=A0 If the strong one isn't >> > caught, >> > the script will terminate as if it was an E_ERROR. >> > >> > I think this would be the most sensible approach in terms of balancing >> > simplicity and flexibility. >> > >> > --Kris >> > >> > >> > >> > On Tue, Feb 28, 2012 at 3:08 PM, Michael Morris >> > wrote: >> >> >> >> Agreed. =A0If conversion can occur without data loss (that is, if the >> >> value being assigned is =3D=3D the value that actually IS assigned) t= hen >> >> no error should occur. >> >> >> >> So >> >> >> >> int $a =3D "1"; // no error. =A01 =3D=3D "1" so who cares? >> >> int $a =3D 'House'; // error 0 !=3D 'House', so this is a problem. >> >> >> >> Again, errors should only raise if the final value !=3D source value. >> >> >> >> On Tue, Feb 28, 2012 at 6:03 PM, Rick WIdmer >> >> >> >> wrote: >> >> > On 2/28/2012 2:58 PM, Kris Craig wrote: >> >> > >> >> >> strong int $a =3D "1"; // Converts to 1. =A0May or may not throw a= n >> >> >> error >> >> >> (I'm >> >> >> still on the fence). >> >> > >> >> > >> >> > It this is an error, it is no longer PHP. >> >> > >> >> > >> >> > -- >> >> > PHP Internals - PHP Runtime Development Mailing List >> >> > To unsubscribe, visit: http://www.php.net/unsub.php >> >> > >> >> >> >> -- >> >> PHP Internals - PHP Runtime Development Mailing List >> >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> >> > > >