Req #80697 [Com]: Return type declaration int allows floats to be returned
| From: | rtrtrtrtrt at dfdfdfdf dot dfd35 | Date: | Tue, 02 Feb 2021 12:45:48 +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-231885@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: Open
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:
> 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
Previous Comments:
------------------------------------------------------------------------
[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