Req #80697 [Com]: Return type declaration int allows floats to be returned
| From: | rtrtrtrtrt at dfdfdfdf dot dfd35 | Date: | Tue, 02 Feb 2021 17:25:14 +0000 |
| Subject: | Req #80697 [Com]: Return type declaration int allows floats to be returned | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-231891@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80697&edit=1
ID: 80697
Comment by: rtrtrtrtrt at dfdfdfdf dot dfd35
Reported by: david dot m dot wakelin at gmail dot com
Summary: Return type declaration int allows floats to be
returned
Status: Closed
Type: Feature/Change Request
Package: *Programming Data Structures
Operating System: Ubuntu 18.04.5 LTS
PHP Version: 8.0.1
Block user comment: N
Private report: N
New Comment:
> believe it can function in a more robust manner
consistency matters!
adjust behavior here and there because it may make sense for badly written code leads to a total
mess
Previous Comments:
------------------------------------------------------------------------
[2021-02-02 17:18:07] david dot m dot wakelin at gmail dot com
I'm well aware of how this works, I know it's fully documented and I know why it works
that way.
I'm not asking for help here. I'm starting a discussion on the possibility of changing the
current behavior as I believe it can function in a more robust manner.
However, after some more thought, I don't think this is viable, so I am closing this report.
------------------------------------------------------------------------
[2021-02-02 13:04:12] sdsdsdsdsd at dfgfgfgfg dot net
in other words:
a) if you want strict_types hebavior enable strict_types
b) if you can't enable it because of code-mess fix your codebase
in weak mode your code literally means and that is how it's supposed to be and your code is in
fact written
public static function getInt(): int
{
return (int)5.67;
}
------------------------------------------------------------------------
[2021-02-02 12:45:48] rtrtrtrtrt at dfdfdfdf dot dfd35
> and not just when using strict_types
nosense - that's exactly what strict_types is for
without it#s implicit casting as desigend and documented
------------------------------------------------------------------------
[2021-02-02 12:38:21] david dot m dot wakelin at gmail dot com
Description:
------------
When specifying int as a return type declaration, a type of float is allowed to be returned, which
gets cast to an int.
All other type coercions preserve the meaning of the returned data, whereas coercing float to int
will lose information after the decimal, therefore not returning the true meaning.
This is not a good feature in my opinion and has been the cause of a couple of uncaught bugs for me.
I suggest that a float to int should be non-coercible in return types and should raise a TypeError
as standard and not just when using strict_types.
Test script:
---------------
class Test
{
public static function getInt(): int
{
return 5.67;
}
}
Test::getInt(); // 5 but would like to see a TypeError
Expected result:
----------------
getInt returns a 5, but I think it should raise a TypeError
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80697&edit=1