Hi,
As PHP 7 currently is, if you write this:
public function foo(): integer {
return 12;
}
$bar = foo();
you'll get the following error:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, integer returned
Similarly, if you were to write this:
public function foo(): boolean {
return true;
}
$bar = $foo();
you'd get this:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of boolean, boolean returned
These are confusing and unhelpful error messages. The long-form names of
types (integer, boolean) aren't accepted as type hints, only their
short-form names (int, bool) are. Yet we didn't reserve the names
'integer' and 'boolean' so we could produce a helpful error message, so
anyone who types these long names will have them interpreted as type
hints for (likely non-existant) classes named 'integer' or 'boolean'
respectively. That produces confusing error messages on its own, but
worse, type hint errors use 'integer' and 'boolean' to describe the
types of values expected or given. Together, these two issues mean you
get the two incredibly confusing error messages above.
This is not kind to the users of PHP. It will cause unneeded confusion.
This isn't even a theoretical case, that first example was based on a
real StackOverflow question.[0] Mistakenly typing 'integer' or 'boolean'
instead of 'int' and 'bool' is not at all an unlikely error, especially
since the PHP manual uses these in some places (we should probably fix
that at some point, and make it use PHP's actual syntax for return types
and variadics, but I digress...), and since the aforementioned type
error messages use them.
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. This would catch out mistakes with people writing the wrong name
for the type hint, and as a bonus would prevent people from misreading
typehints for classes which actually have thse names, as no class with
that name would be allowed. However, we're getting close to release and
this might force some people to rename classes again if changed, causing
disruption, so we might not be able to do this.
Alongside or instead of that, though, we can do two other things to make
this situation better.
First, we could make type error messages use 'int' and 'bool'. This
feels somewhat wrong to me since integer and Boolean are the proper
English names for the types. But we do use the short names in some other
places, like var_dump, so this isn't so bad. This way, we get this
slightly clearer error:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned
Second, we could add a helpful note to the TypeError message when the
type hint is for a class named 'integer' or 'boolean' and the value
passed was an integer or Boolean, respectively. My patch (based on
Anthony's original) for the earlier Scalar Type Hinting with Cast RFC
did this. This would make things even clearer:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned (did you mean to use the type hint 'int'?)
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.
By the way, this situation is at least partly my fault. For PHP 7 I
fixed zend_parse_parameters and userland type hint errors consistent
with regard to type names: always 'integer', never 'long' (which only
ever occured for the expectation, not what was actually given,
bizarrely), and also always 'float', never 'double'. I also made sure
is_int was an alias of is_long and not vice-versa. Alas, I made the
mistake of picking 'int' over 'integer' and 'bool' over 'boolean'.
Anyway, can we do something about this soon?
Thanks!
[0]
http://stackoverflow.com/questions/32665818/php-7-return-type-declarations-fatal-error
--
Andrea Faulds
http://ajf.me/
Hi,
As PHP 7 currently is, if you write this:
public function foo(): integer { return 12; } $bar = foo();
you'll get the following error:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, integer returned
Similarly, if you were to write this:
public function foo(): boolean { return true; } $bar = $foo();
you'd get this:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of boolean, boolean returned
These are confusing and unhelpful error messages. The long-form names of
types (integer, boolean) aren't accepted as type hints, only their
short-form names (int, bool) are. Yet we didn't reserve the names 'integer'
and 'boolean' so we could produce a helpful error message, so anyone who
types these long names will have them interpreted as type hints for (likely
non-existant) classes named 'integer' or 'boolean' respectively. That
produces confusing error messages on its own, but worse, type hint errors
use 'integer' and 'boolean' to describe the types of values expected or
given. Together, these two issues mean you get the two incredibly confusing
error messages above.This is not kind to the users of PHP. It will cause unneeded confusion.
This isn't even a theoretical case, that first example was based on a real
StackOverflow question.[0] Mistakenly typing 'integer' or 'boolean' instead
of 'int' and 'bool' is not at all an unlikely error, especially since the
PHP manual uses these in some places (we should probably fix that at some
point, and make it use PHP's actual syntax for return types and variadics,
but I digress...), and since the aforementioned type error messages use
them.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. This
would catch out mistakes with people writing the wrong name for the type
hint, and as a bonus would prevent people from misreading typehints for
classes which actually have thse names, as no class with that name would be
allowed. However, we're getting close to release and this might force some
people to rename classes again if changed, causing disruption, so we might
not be able to do this.Alongside or instead of that, though, we can do two other things to make
this situation better.First, we could make type error messages use 'int' and 'bool'. This feels
somewhat wrong to me since integer and Boolean are the proper English names
for the types. But we do use the short names in some other places, like
var_dump, so this isn't so bad. This way, we get this slightly clearer
error:Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned
Second, we could add a helpful note to the TypeError message when the type
hint is for a class named 'integer' or 'boolean' and the value passed was
an integer or Boolean, respectively. My patch (based on Anthony's original)
for the earlier Scalar Type Hinting with Cast RFC did this. This would make
things even clearer:Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned (did you mean to use the type hint 'int'?)
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.By the way, this situation is at least partly my fault. For PHP 7 I fixed
zend_parse_parameters and userland type hint errors consistent with regard
to type names: always 'integer', never 'long' (which only ever occured for
the expectation, not what was actually given, bizarrely), and also always
'float', never 'double'. I also made sure is_int was an alias of is_long
and not vice-versa. Alas, I made the mistake of picking 'int' over
'integer' and 'bool' over 'boolean'.Anyway, can we do something about this soon?
Thanks!
[0]
http://stackoverflow.com/questions/32665818/php-7-return-type-declarations-fatal-error
Hi Andrea,
Can you go ahead and make a branch/PR to add these type aliases in? There's
no right or wrong answer to 'int' versus 'integer', so allowing devs to
continue to use either will be welcoming.
--
Andrea Faulds
http://ajf.me/
Hi Paul,
Paul Dragoonis wrote:
Hi Andrea,
Can you go ahead and make a branch/PR to add these type aliases in? There's
no right or wrong answer to 'int' versus 'integer', so allowing devs to
continue to use either will be welcoming.
I've written a pull request (doesn't have tests yet) that makes the
other type names produce helpful error messages:
https://github.com/php/php-src/pull/1572
e.g.
$ ./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
This would completely solve the confusion issue, I think. A crystal
clear error message.
As I've said in a different email, I don't like the idea of allowing the
aliases because it'd be another set of coding style differences we don't
need. This lets us pick one name and stick with it, without causing
confusion.
Thanks.
--
Andrea Faulds
http://ajf.me/
(Sorry Andrea, I'm picking on your e-mail because it's easiest, but
it's a general response to the thread.)
e.g.
$ ./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
This would completely solve the confusion issue, I think. A crystal clear
error message.
It is a good error message, but I disagree with the premise behind it:
As I've said in a different email, I don't like the idea of allowing the
aliases because it'd be another set of coding style differences we don't
need. This lets us pick one name and stick with it, without causing
confusion.
I agree that we should do something, but I think we should alias.
We allow both "int" and "integer" in settype()
and we allow it in type
casting — the two other places where a user can specify a type for
conversion. I still think it's a poor choice to not allow both in type
declarations: while I'm generally a fan of having one way to do
things, I believe that the inconsistency in the language is worse than
the potential ambiguity in style guides.
Hell, I still can't remember which out of "int" and "integer" is the
right one, and I've now written a decent amount of PHP 7 code and
wrote half of the documentation for this.
Plus, if we error when "integer" is used, we've moved people's cheese
anyway (by disallowing the class name). Let's not compound that by
forcing them to do busywork.
Adam, who hopes that anecdote doesn't say more about his working
memory than the design of the language.
Hi Adam,
Adam Harvey wrote:
(Sorry Andrea, I'm picking on your e-mail because it's easiest, but
it's a general response to the thread.)
Ah, don't worry about it.
I agree that we should do something, but I think we should alias.
We allow both "int" and "integer" in
settype()
and we allow it in type
casting — the two other places where a user can specify a type for
conversion.
We also support 'real' and 'binary' in casts, but these are quite rare,
and I think (int) is a lot more common than (integer), heck, the manual
uses it.
I still think it's a poor choice to not allow both in type
declarations: while I'm generally a fan of having one way to do
things, I believe that the inconsistency in the language is worse than
the potential ambiguity in style guides.
Well, we could support the full set of names (int, integer, long, float,
double, real, string, binary, bool, boolean) everywhere, but this is a
bit unruly. It's better to pick one and stick to it, and discourage the
us of the others or slowly phase them out.
Hell, I still can't remember which out of "int" and "integer" is the
right one, and I've now written a decent amount of PHP 7 code and
wrote half of the documentation for this.
The rule seems to be 'integer' in English, 'int' in code, for the most part.
Plus, if we error when "integer" is used, we've moved people's cheese
anyway (by disallowing the class name). Let's not compound that by
forcing them to do busywork.
I think this would drill into you that it's 'int' not 'integer' pretty
quickly, so it wouldn't become annoying.
Adam, who hopes that anecdote doesn't say more about his working
memory than the design of the language.
PHP is a confusing behemoth, don't worry, you're not alone :)
Thanks.
--
Andrea Faulds
http://ajf.me/
Hi Adam,
Adam Harvey wrote:
(Sorry Andrea, I'm picking on your e-mail because it's easiest, but
it's a general response to the thread.)Ah, don't worry about it.
I agree that we should do something, but I think we should alias.
We allow both "int" and "integer" in
settype()
and we allow it in type
casting — the two other places where a user can specify a type for
conversion.We also support 'real' and 'binary' in casts, but these are quite rare,
and I think (int) is a lot more common than (integer), heck, the manual
uses it.I still think it's a poor choice to not allow both in type
declarations: while I'm generally a fan of having one way to do
things, I believe that the inconsistency in the language is worse than
the potential ambiguity in style guides.Well, we could support the full set of names (int, integer, long, float,
double, real, string, binary, bool, boolean) everywhere, but this is a bit
unruly. It's better to pick one and stick to it, and discourage the us of
the others or slowly phase them out.
I'm going to keep beating this drum, but I disagree with that last
statement. We should at the very least support both full and short names
for "int" and "bool": again, why stomp on people's muscle memory over
something so utterly trivial to support? The only argument that I've
gleaned from this thread is "but, but, it's inconsistent" and that is not
something I disagree with at all. I simply prefer to support them, clearly
most others here don't.
An arbitrary code search shows that, for people currently documenting their
arguments with phpdoc, a non-trivial number of people choose the longer
terms for bool/int. [1] [2]
Hell, I still can't remember which out of "int" and "integer" is the
right one, and I've now written a decent amount of PHP 7 code and
wrote half of the documentation for this.
This. So much this. Just like you, I keep typing "integer", and "boolean".
Maybe us folks on the documentation team have some mental deficiency that
the more internals folks don't have? (I'm only half-joking there.)
The rule seems to be 'integer' in English, 'int' in code, for the most
part.
I'd agree with "for the most part" but I disagree with that part being a
large enough part to ignore the other parts.
Plus, if we error when "integer" is used, we've moved people's cheese
anyway (by disallowing the class name). Let's not compound that by
forcing them to do busywork.I think this would drill into you that it's 'int' not 'integer' pretty
quickly, so it wouldn't become annoying.
It's int. It's int. It's int. It's int. For this to be resolved, at least
for me, personally, there's going to need to be a lot more drilling. I like
to think that I'm not alone in this respect, but maybe that's not the case.
Adam, who hopes that anecdote doesn't say more about his working
memory than the design of the language.
Peter, who is a little tired of the folks beating the "more errors please"
drum.
PHP is a confusing behemoth, don't worry, you're not alone :)
Thanks.
--
Andrea Faulds
http://ajf.me/--
[1] "@param boolean"
https://github.com/search?l=php&q=%22%40param+boolean%22&ref=searchresults&type=Code&utf8=%E2%9C%93
[2] "@param integer"
https://github.com/search?l=php&q=%22%40param+integer%22&ref=searchresults&type=Code&utf8=%E2%9C%93
Hi,
As PHP 7 currently is, if you write this:
public function foo(): integer { return 12; } $bar = foo();
you'll get the following error:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, integer returned
Similarly, if you were to write this:
public function foo(): boolean { return true; } $bar = $foo();
you'd get this:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of boolean, boolean returned
...
Alongside or instead of that, though, we can do two other things to make
this situation better.First, we could make type error messages use 'int' and 'bool'. This
feels somewhat wrong to me since integer and Boolean are the proper
English names for the types. But we do use the short names in some other
places, like var_dump, so this isn't so bad. This way, we get this
slightly clearer error:Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned
Second, we could add a helpful note to the TypeError message when the
type hint is for a class named 'integer' or 'boolean' and the value
passed was an integer or Boolean, respectively. My patch (based on
Anthony's original) for the earlier Scalar Type Hinting with Cast RFC
did this. This would make things even clearer:Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned (did you mean to use the type hint
'int'?)Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.
Additionally, we could say "instance of class ...".
--
Regards,
Mike
Hi Michael,
Michael Wallner wrote:
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.Additionally, we could say "instance of class ...".
We could do that, though it's already 'instance of'. Not sure on this one.
Thanks.
--
Andrea Faulds
http://ajf.me/
Andrea Faulds wrote on 13/10/2015 12:00:
Hi Michael,
Michael Wallner wrote:
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.Additionally, we could say "instance of class ...".
We could do that, though it's already 'instance of'. Not sure on this
one.
It's not necessarily a class you're requiring an instance of, it could
(and according to some design disciplines "should") be an interface. I
think the "did you mean..." is more useful if we don't make everything
alias.
Regards,
Rowan Collins
[IMSoP]
All,
Andrea Faulds wrote on 13/10/2015 12:00:
Hi Michael,
Michael Wallner wrote:
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.Additionally, we could say "instance of class ...".
We could do that, though it's already 'instance of'. Not sure on this one.
It's not necessarily a class you're requiring an instance of, it could (and
according to some design disciplines "should") be an interface. I think the
"did you mean..." is more useful if we don't make everything alias.Regards,
Rowan Collins
[IMSoP]
I think it's important to clean up these error messages and make it
easier to find the root problem.
With that said, I strongly believe that adding new reserved classes is
the wrong way to do that. Part of the problem is inconsistency around
what we call types in the first place. This leads to confusion around
what types people expect and how they expect them.
An example is the fact that types are different all over the place.
Cast tokens, the return of gettype()
, documentation, etc.
Everything is inconsistent. And reserving extra types because of this
inconsistency does nothing to actually fix the problem.
Instead, my suggestion would be to double-down on clarifying that
"int"/"float"/"bool"/etc are the canonical correct versions, and
everything else is an alias. In future versions (likely 8, but
possibly earlier) we could deprecate the aliases, to keep things far
more consistent.
Reserving more classes now (both 7.0 and this late in the RC process)
is IMHO not a great idea. Either side sucks (whichever we do we're
wrong), but at least not reserving gives us a path towards cleaning it
up rather than making it worse.
I like Derick's suggestion (though removing "class"):
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of boolean, scalar boolean returned
The "instance" says it's an object.
The rest of it becomes a documentation problem. We're already mostly
consistent in the docs (all of the signatures use the correct tokens),
so it shouldn't be hard to finish that off...
My $0.02 at least.
Anthony
On Tue, Oct 13, 2015 at 6:40 PM, Anthony Ferrara ircmaxell@gmail.com
wrote:
All,
On Tue, Oct 13, 2015 at 9:56 AM, Rowan Collins rowan.collins@gmail.com
wrote:Andrea Faulds wrote on 13/10/2015 12:00:
Hi Michael,
Michael Wallner wrote:
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.Additionally, we could say "instance of class ...".
We could do that, though it's already 'instance of'. Not sure on this
one.It's not necessarily a class you're requiring an instance of, it could
(and
according to some design disciplines "should") be an interface. I think
the
"did you mean..." is more useful if we don't make everything alias.Regards,
Rowan Collins
[IMSoP]I think it's important to clean up these error messages and make it
easier to find the root problem.With that said, I strongly believe that adding new reserved classes is
the wrong way to do that. Part of the problem is inconsistency around
what we call types in the first place. This leads to confusion around
what types people expect and how they expect them.An example is the fact that types are different all over the place.
Cast tokens, the return ofgettype()
, documentation, etc.Everything is inconsistent. And reserving extra types because of this
inconsistency does nothing to actually fix the problem.Instead, my suggestion would be to double-down on clarifying that
"int"/"float"/"bool"/etc are the canonical correct versions, and
everything else is an alias. In future versions (likely 8, but
possibly earlier) we could deprecate the aliases, to keep things far
more consistent.Reserving more classes now (both 7.0 and this late in the RC process)
is IMHO not a great idea. Either side sucks (whichever we do we're
wrong), but at least not reserving gives us a path towards cleaning it
up rather than making it worse.I like Derick's suggestion (though removing "class"):
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of boolean, scalar boolean returned
I also don't like reserving new class names or limiting their use.
The message above looks clear and it shouldn't be a big problem to
implement the fix.
This fix shouldn't change anything except error message, so it'll make
minimal risk for 7.0 release process.
Thanks. Dmitry.
The "instance" says it's an object.
The rest of it becomes a documentation problem. We're already mostly
consistent in the docs (all of the signatures use the correct tokens),
so it shouldn't be hard to finish that off...My $0.02 at least.
Anthony
Hi Dmitry,
Dmitry Stogov wrote:
I also don't like reserving new class names or limiting their use.
The message above looks clear and it shouldn't be a big problem to
implement the fix.
This fix shouldn't change anything except error message, so it'll make
minimal risk for 7.0 release process.
Just changing error messages would work, but it's an unclean solution.
It would be changing existing naming conventions ('integer' in English,
'int' in code). Still, it's better than nothing if we can't reserve the
names.
Thanks.
--
Andrea Faulds
http://ajf.me/
Hi Andrea,
-----Original Message-----
From: Andrea Faulds [mailto:ajf@ajf.me]
Sent: Wednesday, October 14, 2015 12:53 AM
To: internals@lists.php.net
Subject: Re: [PHP-DEV] Scalar type hints and scalar type name aliases
causeconfusonHi Dmitry,
Dmitry Stogov wrote:
I also don't like reserving new class names or limiting their use.
The message above looks clear and it shouldn't be a big problem to
implement the fix.
This fix shouldn't change anything except error message, so it'll make
minimal risk for 7.0 release process.Just changing error messages would work, but it's an unclean solution.
It would be changing existing naming conventions ('integer' in English, 'int' in
code). Still, it's better than nothing if we can't reserve the names.
People already ported a lot of stuff to 7.0, or get already trained, etc. So changing the error message looks like the only how far we can go at current stage. It is clear enough to express the necessary error, and it is soft enough to will possibly break some unit tests but not the actual codes.
Thanks
Anatol
Hi,
As PHP 7 currently is, if you write this:
public function foo(): integer { return 12; } $bar = foo();
you'll get the following error:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, integer returned
Similarly, if you were to write this:
public function foo(): boolean { return true; } $bar = $foo();
you'd get this:
Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of boolean, boolean returned
These are confusing and unhelpful error messages. The long-form names of
types (integer, boolean) aren't accepted as type hints, only their
short-form names (int, bool) are. Yet we didn't reserve the names 'integer'
and 'boolean' so we could produce a helpful error message, so anyone who
types these long names will have them interpreted as type hints for (likely
non-existant) classes named 'integer' or 'boolean' respectively. That
produces confusing error messages on its own, but worse, type hint errors
use 'integer' and 'boolean' to describe the types of values expected or
given. Together, these two issues mean you get the two incredibly confusing
error messages above.This is not kind to the users of PHP. It will cause unneeded confusion.
This isn't even a theoretical case, that first example was based on a real
StackOverflow question.[0] Mistakenly typing 'integer' or 'boolean' instead
of 'int' and 'bool' is not at all an unlikely error, especially since the
PHP manual uses these in some places (we should probably fix that at some
point, and make it use PHP's actual syntax for return types and variadics,
but I digress...), and since the aforementioned type error messages use
them.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. This
would catch out mistakes with people writing the wrong name for the type
hint, and as a bonus would prevent people from misreading typehints for
classes which actually have thse names, as no class with that name would be
allowed. However, we're getting close to release and this might force some
people to rename classes again if changed, causing disruption, so we might
not be able to do this.Alongside or instead of that, though, we can do two other things to make
this situation better.First, we could make type error messages use 'int' and 'bool'. This feels
somewhat wrong to me since integer and Boolean are the proper English names
for the types. But we do use the short names in some other places, like
var_dump, so this isn't so bad. This way, we get this slightly clearer
error:Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned
Second, we could add a helpful note to the TypeError message when the type
hint is for a class named 'integer' or 'boolean' and the value passed was
an integer or Boolean, respectively. My patch (based on Anthony's original)
for the earlier Scalar Type Hinting with Cast RFC did this. This would make
things even clearer:Fatal error: Uncaught TypeError: Return value of foo() must be an
instance of integer, int returned (did you mean to use the type hint 'int'?)
I'm sure the "hint" would will get certain nay-sayers' skins crawling.
Even if we can't reserve the names, I hope we can do the two other
suggestions in time for release.By the way, this situation is at least partly my fault. For PHP 7 I fixed
zend_parse_parameters and userland type hint errors consistent with regard
to type names: always 'integer', never 'long' (which only ever occured for
the expectation, not what was actually given, bizarrely), and also always
'float', never 'double'. I also made sure is_int was an alias of is_long
and not vice-versa. Alas, I made the mistake of picking 'int' over
'integer' and 'bool' over 'boolean'.Anyway, can we do something about this soon?
I would much rather we use the full names for these types across the board.
The manual uses them almost exclusively, and I'd hazard a bet that it is
many peoples' first choice when trying out the argument/return type
declarations.
Failing that, at least making them available as aliases is a good thing in
my book. I never understood the reluctance to make use of all existing
names when the type declarations discussions were going on. Fact is, people
are going to type "double", "long", etc. in their declarations... why get
in their way?
Thanks!
[0]
http://stackoverflow.com/questions/32665818/php-7-return-type-declarations-fatal-error--
Andrea Faulds
http://ajf.me/
Hi Peter,
Peter Cowburn wrote:
I would much rather we use the full names for these types across the board.
I would mostly agree with you. Using the full English names whenever
we're writing English, not code, makes sense. But we usually use
int/float/etc. in code, like other languages do, and I don't see a good
reason to change that.
The manual uses them almost exclusively,
Not for type signatures it doesn't. It usually uses "int" and "bool".
and I'd hazard a bet that it is
many peoples' first choice when trying out the argument/return type
declarations.
Yeah, and others will sometimes use them by 'mistake'. I know I
certainly have. So we need to prevent this creating confusion.
Failing that, at least making them available as aliases is a good thing in
my book. I never understood the reluctance to make use of all existing
names when the type declarations discussions were going on. Fact is, people
are going to type "double", "long", etc. in their declarations... why get
in their way?
I don't want to allow them as aliases, because it means another thing to
add to style guides. Some people would use 'float', some 'double', some
would use 'int', some 'integer', some 'long'. It'd be much cleaner to
just pick one and stick with it. Types don't need multiple names.
However, as you mention, many people are used to the other variants. So
I'd like to reserve them and throw an error if you use them. Much better
than people typing 'function format_number(double $number, long
$digits)` and getting bewildered when it doesn't work.
Thanks.
--
Andrea Faulds
http://ajf.me/
Peter Cowburn wrote:
Failing that, at least making them available as aliases is a good
thing in my book. I never understood the reluctance to make use of
all existing names when the type declarations discussions were going
on. Fact is, people are going to type "double", "long", etc. in
their declarations... why get in their way?I don't want to allow them as aliases, because it means another thing
to add to style guides. Some people would use 'float', some 'double',
some would use 'int', some 'integer', some 'long'. It'd be much
cleaner to just pick one and stick with it. Types don't need multiple
names.
Having aliases is probably what I feel most strongly against as well.
cheers,
Derick
--
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine
Hi Peter,
Peter Cowburn wrote:
I would much rather we use the full names for these types across the
board.I would mostly agree with you. Using the full English names whenever we're
writing English, not code, makes sense. But we usually use int/float/etc.
in code, like other languages do, and I don't see a good reason to change
that.The manual uses them almost exclusively,
Not for type signatures it doesn't. It usually uses "int" and "bool".
I said "almost" for a reason. More clearly, we use the long names in prose
and by convention the short ones in the function prototypes.
and I'd hazard a bet that it is
many peoples' first choice when trying out the argument/return type
declarations.Yeah, and others will sometimes use them by 'mistake'. I know I certainly
have. So we need to prevent this creating confusion.
I would say that more confusion will arise from trying to use something
that comes naturally (e.g. "integer") and getting an error, than having it
"just work".
Failing that, at least making them available as aliases is a good thing in
my book. I never understood the reluctance to make use of all existing
names when the type declarations discussions were going on. Fact is,
people
are going to type "double", "long", etc. in their declarations... why get
in their way?I don't want to allow them as aliases, because it means another thing to
add to style guides. Some people would use 'float', some 'double', some
would use 'int', some 'integer', some 'long'. It'd be much cleaner to just
pick one and stick with it. Types don't need multiple names.
So what if there is more than one way to say "hey, I want an integer here",
particularly when they're super-duper common terms that are used
everywhere. (I'm talking about "boolean" and "integer" specifically,
however if someone wants to use "long", or "double", or "real" then why the
hell stop them?
However, as you mention, many people are used to the other variants. So
I'd like to reserve them and throw an error if you use them. Much better
than people typing 'function format_number(double $number, long $digits)`
and getting bewildered when it doesn't work.
-1 to throwing an error, that's just completely the wrong direction in my
view. I'd much rather your example work.
Thanks.
--
Andrea Faulds
http://ajf.me/
Hi,
Peter Cowburn wrote:
Hi Peter,
Peter Cowburn wrote:
The manual uses them almost exclusively,
Not for type signatures it doesn't. It usually uses "int" and "bool".
I said "almost" for a reason. More clearly, we use the long names in prose
and by convention the short ones in the function prototypes.
Oh, right. My bad.
I would say that more confusion will arise from trying to use something
that comes naturally (e.g. "integer") and getting an error, than having it
"just work".
People might be slightly surprised it's not allowed, but it'd be very
clear what was wrong with the code and how to fix it, so it's not really
a problem.
So what if there is more than one way to say "hey, I want an integer here",
particularly when they're super-duper common terms that are used
everywhere. (I'm talking about "boolean" and "integer" specifically,
however if someone wants to use "long", or "double", or "real" then why the
hell stop them?
They're not used in source code much, though.
Thanks.
--
Andrea Faulds
http://ajf.me/
<snip>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.
This is also an issue for argument typehints:
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 <filename> on line
<x>")
for:
<?php
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?
This would catch out mistakes with people writing the wrong name for
the type hint, and as a bonus would prevent people from misreading
typehints for classes which actually have thse names, as no class with
that name would be allowed.
However, we're getting close to release and this might force some people to
rename classes again if changed, causing disruption, so we might not be able
to do this.
Well, that's what we have RCs for. And there are a few more coming up!
Alongside or instead of that, though, we can do two other things to make this
situation better.First, we could make type error messages use 'int' and 'bool'. This feels
somewhat wrong to me since integer and Boolean are the proper English names
for the types. But we do use the short names in some other places, like
var_dump, so this isn't so bad. This way, we get this slightly clearer error:Fatal error: Uncaught TypeError: Return value of foo() must be an instance
of integer, int returned
Second, we could add a helpful note to the TypeError message when the type
hint is for a class named 'integer' or 'boolean' and the value passed was an
integer or Boolean, respectively. My patch (based on Anthony's original) for
the earlier Scalar Type Hinting with Cast RFC did this. This would make things
even clearer:Fatal error: Uncaught TypeError: Return value of foo() must be an instance
of integer, int returned (did you mean to use the type hint 'int'?)
I'd find that nannying :)
Anyway, can we do something about this soon?
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
cheers,
Derick
2015-10-13 13:08 GMT+02:00 Derick Rethans derick@php.net:
What about double vs float? Another common combination.
(or "real" -- just to compliment the typecast)
</joke> =P
--
regards,
Kalle Sommer Nielsen
kalle@php.net
Hi Derick,
Derick Rethans wrote:
<snip>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 <filename> on line
<x>")
It does. Could be improved slightly by the addition of a comma before
the second 'in'.
for:
<?php
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/