Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75559 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17029 invoked from network); 15 Jul 2014 19:45:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2014 19:45:10 -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.200 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.200 imap1-2.ox.privateemail.com Received: from [192.64.116.200] ([192.64.116.200:39632] helo=imap1-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CB/76-15121-FB485C35 for ; Tue, 15 Jul 2014 15:45:04 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id DA0EDB00081; Tue, 15 Jul 2014 15:45:00 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap1.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap1.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id x4jFfTzawveY; Tue, 15 Jul 2014 15:45:00 -0400 (EDT) Received: from [192.168.0.15] (unknown [90.210.122.167]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id C4427B0007B; Tue, 15 Jul 2014 15:44:59 -0400 (EDT) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) In-Reply-To: <53C57A02.8020001@gmail.com> Date: Tue, 15 Jul 2014 20:44:55 +0100 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <1366DEE2-8B5C-4814-89A5-0A191F963CB4@ajf.me> References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <16D48604-0C0A-4613-91A4-21392E3A2636@ajf.me> <53C3DB42.7090205@gmail.com> <3158105795AAC1408619EC8BA03384A93D0214F5@EXC-PUB01A.leedsmet.ac.uk> <53C5673D.8030201@gmail.com> <99B9138F-3E18-4DF6-8934-DADD6C99E438@ajf.me> <53C57A02.8020001@gmail.com> To: Rowan Collins X-Mailer: Apple Mail (2.1878.6) Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: ajf@ajf.me (Andrea Faulds) On 15 Jul 2014, at 19:59, Rowan Collins wrote: > Or, to implement a "safe cast", and catch the error case: >=20 > if ( false =3D=3D=3D $foo =3D filter_var($foo, FILTER_VALIDATE_INT) ) = { > $foo =3D 1; > } >=20 > Yuck! :P The patch and RFC introduce a new set of =93safe=94 conversion functions = which we could use for these safe casts. In fact, it makes normal casts = use them too now, they just ignore the errors. PHP has pseudo-functions, maybe we could use that format? = cast_int($foo)? Actually, perhaps the solution is an ahead-of-time check: if (safe_cast($foo, int)) { $bar =3D (int!)$foo; } else { $bar =3D 1; } Perhaps safe_int or something would be better. Anyhow, (int!) would = error if the cast was unsafe, but as you'd checked ahead of time, it = wouldn=92t error here. That, or perhaps something with an optional argument: $bar =3D cast_int($foo, 1); // defaults to 1 if cast unsafe $bar =3D cast_int($foo); // unsafe cast But I don=92t like that approach as it doesn=92t match the nice (int!) = syntax. This just occurred to me: $bar =3D (int?)$foo ? (int!)$foo : 1; I=92m not sure if that=92s horrible or brilliant. -- Andrea Faulds http://ajf.me/