Req #80697 [Com]: Return type declaration int allows floats to be returned

From: Date: Tue, 02 Feb 2021 13:04:12 +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-231887@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: sdsdsdsdsd at dfgfgfgfg dot net 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: 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; } Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#231887) next »