Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88775 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7959 invoked from network); 13 Oct 2015 13:44:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Oct 2015 13:44:54 -0000 X-Host-Fingerprint: 178.62.40.5 ajf.me Received: from [178.62.40.5] ([178.62.40.5:3324] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B1/70-05441-5DA0D165 for ; Tue, 13 Oct 2015 09:44:53 -0400 Message-ID: To: internals@lists.php.net References: <5C.21.16518.AA80C165@pb1.pair.com> Date: Tue, 13 Oct 2015 14:44:49 +0100 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0 SeaMonkey/2.38 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 178.62.40.5 Subject: Re: [PHP-DEV] Scalar type hints and scalar type name aliases causeconfuson From: ajf@ajf.me (Andrea Faulds) Hi Derick, Derick Rethans wrote: > On Mon, 12 Oct 2015, Andrea Faulds wrote: > >> you'll get the following error: >> >> Fatal error: Uncaught TypeError: Return value of foo() must be an instance >> of integer, integer returned > > > >> you'd get this: >> >> Fatal error: Uncaught TypeError: Return value of foo() must be an instance >> of boolean, boolean returned > > What about double vs float? Another common combination. Oh yeah, that's a bit of an issue. Since I changed it, PHP 7 doesn't say 'double' in its type error messages any more, but there's still going to be people who'd expect that to work. So, my patch to reserve and produce errors includes 'double'. > > This is also an issue for argument typehints: Exactly the same issue, yes. I just used return types for my examples, sorry if that was unclear. > > TypeError: Argument 1 passed to foo() must be an instance of integer, > integer given, called in /tmp/scalar.php on line 7 in /tmp/scalar.php on > line 3 > > (I also think, this reads odd with two times "in on line > ") > It does. Could be improved slightly by the addition of a comma before the second 'in'. > for: > > declare(type=strict); > function foo(integer $a) { } > foo(42); > ?> > >> To avoid confusion, I would like it if we reserve 'integer' and 'boolean' >> alongside 'int' and 'bool', and make them produce an error if used. > > With a parse error mentioning 'illegal token' or something like that? Yeah. My current patch does this: ./sapi/cli/php -r 'function foo(): long {}' Fatal error: 'long' is not a valid type hint, use 'int' instead in Command line code on line 1 If you try to use it as a the name of a class/interface/trait, it produces the usual error for reserved class name. > > Well, that's what we have RCs for. And there are a few more coming up! > Yes, perhaps I shouldn't give up just yet. > > I'd find that nannying :) > It *is* a bit annoying, especially if you actually intended to have a class by that name. > > I'd prefer reservation of integer and boolean, but a better error > message is okay too. I'd go with something like: > > Fatal error: Uncaught TypeError: Return value of foo() must be an instance of class boolean, scalar boolean returned > That sounds good, albeit a little verbose. Thanks! -- Andrea Faulds http://ajf.me/